Reload interpretation in JavaScript and reload interpretation in javascript

Source: Internet
Author: User

Reload interpretation in JavaScript and reload interpretation in javascript

There is a special data type in JavaScript-Function type. Every Function in JavaScript is a Function-type instance. Because the function is an object, the function name is actually a pointer to the function object and will not be bound to a function.

<pre name="code" class="html">function sum(num1,num2){return num1 +num2;}alert(sum(10,10));        //20var other = sum;alert(other(10,10));        //20sum = null;alert(other(10,10));        //20

 

Using a function name as a pointer to a function helps you understand why ECMAScript does not have the concept of function overloading.

function sum(num1){return num1 +100;} function sum(num1){return num1 +200;}alert(sum(200));        //400


Although two functions with the same name are declared, the subsequent functions overwrite the previous functions. The above is equivalent to the following code:

function sum(num1){return num1 +100;}sum = function(num1){return num1 +200;}alert(sum(200));        //400

When creating the second function, it actually overwrites the referenced first function variable sum.


Can javascript Functions be overloaded?

The reload of javascript Functions is different from that of java functions.
When defining a JavaScript function, the function name is the identifier of the function object. The number of parameters is only the attribute of the function. It is not feasible to implement overload by defining functions with different numbers of parameters.
When calling a function, js finds the corresponding function object through the function name, and matches the function with the expression parameter list in order according to the parameter defined by the function, removing unnecessary parameters, parameters that are not enough are processed by undefined, and then the function code is executed. Therefore, js overload functions must be implemented by determining the parameter values and types through function code.
When defining a function, you need to put the required parameters at the beginning of the parameter list, and the optional parameters are placed after the required parameters in the parameter list to facilitate function overloading.

Jequery or javascript implements partial page Overloading

I don't quite understand why? Hi me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.