JavaScript reference type-function, javascript Reference Function

Source: Internet
Author: User

JavaScript reference type-function, javascript Reference Function

The Function type is one of the ECMAScript reference types. It is a familiar Function. Interestingly, in ECMAScript, a function is actually an object.

Each Function is a Function-type instance and has attributes and methods like other reference types.

Because the function is an object, the function name is actually a pointer to this function object and will not be bound to a function.

Three methods to define a function:

1. Use the function declaration syntax.

function sum(num1,num2){    return num1+num2;}

2. Define using function expressions.
var sum = function(num1,num2){     return num1+num2;};
Use the variable name sum to reference this function, sum ();

Because expressions are defined, do not forget to end the expression with.


3. Use Function to construct a Function. The Function constructor receives any number of parameters and regards the last parameter as the Function body.

However, this method has poor performance and is no longer recommended.

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

Note that the function name is only a pointer to the function object, that is, a variable. A function may have multiple names, just like an object that can be referenced by multiple pointers.

For example, in the sum variable above, when we want to call a function, use the function to add parentheses, sum );. The function name sum without parentheses indicates that we access this function pointer instead of calling the function. Pay attention to this when passing functions as values.


After understanding that the function name is just a pointer, we can understand why there is no function overload in ECMAScript. When we write two functions with the same name, instead, the pointer represented by this function name is directed to the function object written later. The execution result looks like the previous function is replaced by the last function with the same name.


We have mentioned two methods for defining functions: function declaration and function expression. They work the same way when defining functions, but when the parser loads our js Code to the execution environment (such as a browser, the order of loading the two function definitions is different.

The parser first reads the function declaration and allows it to be accessed before executing any code. As for the function expression, it only waits until the parser executes the line of code where it is located, will be interpreted and executed. For example:

Alert (sum (1, 2); // It can be executed // although the function declaration is written after function sum (num1, num2) {return num1 + num2 ;}

But the following won't work:
Alert (sum (); // generate error // function expression var sum = function (num1, num2) {return num1 + num2 ;};

In ECMAScript, the function name itself is a variable, so the function can also be used as a value. That is to say, a function can be passed to another function just like a parameter, and a function can be returned as a result of another function.

function callFunction(function,argument){     return function(argument);}function add(num){    return num + 100;}var result = callFunction(add,100); alert(result);  //200

Within the function, there are two special objects: arguments and this.

The arguments object is similar to an array. It contains all the parameters in the input function and can be accessed using the square brackets of the array. The ECMAScript function does not mind how many parameters are passed in, nor does it care about the type of parameters passed in. You can define a function that receives two parameters, but can pass any parameter in use, or none. Because for the ECMAScript function, the named parameter only facilitates the use of parameters in the function body. It is not necessary, in the function body, you can use the arguments object to obtain each parameter passed to the function.

In addition, the arguments object also has a callee attribute, which is a pointer pointing to a function that owns the arguments object.


Another special object of the function is this. this behavior and this type in Java. This references the environment object where function data is executed. (When a function is called in the global scope, this object references the Global Object window ).

window.color = "red";var o = {color : "yellow"};function sayColor(){    alert(this.color);}sayColor();  //redo.sayColor = sayColor;o.sayColor();  //yellow
Before the sayColor function is called, the value of this is not determined. In the global environment, sayColor () is called. The value of this is window. When o. sayColor () is called, this references object o.


Function Attributes: length and prototype.

The length attribute is relatively simple. It indicates the number of name parameters that the function wants to receive, that is, the number of parameters defined when defining the function.

The prototype attribute is the prototype attribute obtained by the function object. This is introduced when introducing the object. It will not be expanded here.


Function Method:

Each function contains two non-inherited methods: apply () and call ().

The purpose of these two methods is to call a function in a specific scope, which is actually used to set the value of this object in the function body.

The difference between the two methods is that the method for receiving parameters is different. It doesn't matter which method is used when parameters are not passed to the function.

window.color = "red";var o = {color : "yellow"};function sayColor(){    alert(this.color);}sayColor.apply(o);  //yellowsayColor.call(o);  //yellowsayColor();  //redsayColor.call(window);  //red


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.