"JavaScript" function type

Source: Internet
Author: User

In JavaScript, each function is an instance of the functions class.

In other words, each defined function name is an instance of the functions type.

The function name holds a pointer to the function (the type instance pointer).

Can be implemented using the constructor of the function, and the last parameter is treated as a body of functions, such as:

var sum = new Function ("Num1", "num2", "return num1+num2"); Not recommended

  

1. No overloads

  Because a pointer is stored in the function name and the same function name is redefined, the previously stored function is overwritten regardless of whether the parameter signature is consistent.

No overloads exist.

However, the overload of the function can be implemented, JavaScript, when the function passed in the parameters and the number of function signatures are not the same, you can still run (in the function body to determine whether the argument exists in the statement, to avoid errors).

This can also implement overloading of functions.

2. function declaration differs from function expression

  JavaScript compiles the definition of the function in advance, either after the call, or the contents of the function body, such as:

    alert (sum (10,10); Output 20

    function sum (NUM1, num2) {
return NUM1 + num2;

}

  

When you use the expression of a function, the function is defined when it is compiled into an expression, so if the output of the example above is an error, such as:

    alert (sum (10,10); Unexpected identifier

    var sum = function (NUM1, num2) {

return NUM1 + num2;

}

3. Functions as values

  Because a pointer is stored in the function name, the function can be passed in as a parameter inside another function, such as:

    function Callsomefunction (someFunction, someargument) {

Return someFunction (someargument);

}

function Add10 (num) {

return num + 10;

}

var result1 = callsomefunction (ADD10, 10);

alert (RESULT1); 20

function getgreeting (name) {
Return "Hello," + name;

}

var result2 = callsomefunction (Getgreet, "Nicholas");

alert (RESULT2);

4. Function Internal Properties

  

"JavaScript" function type

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.