The function type is one of the ECMAScript reference types, and it is the familiar functions. Interestingly, in ECMAScript, the function is actually an object.
Each of these functions is an instance of a function type and has properties and methods as well as other reference types.
Because the function is an object, the function name is actually a pointer to the function object and is not bound to a function.
There are three ways to define a function:
1. Use function declaration syntax.
function sum (num1,num2) { return num1+num2;}
2. Use the function expression definition.
var sum = function (num1,num2) { return num1+num2;};
This function is referenced by the variable name sum, sum ();
Because it is defined using an expression, do not forget that the expression is to be terminated;
3. Use the function constructor. The function constructor receives any number of arguments and takes the last argument as a function body.
However, the performance of this method is not good, is not recommended to use.
var sum = new Function ("Num1", "num2", "return num1+num2");
We need to note that the function name is just a pointer to the function object, which is a variable, a function may have multiple names, just as an object can be referenced by multiple pointers.
For example, the sum variable above , when we want to call the function, with the function of parentheses, sum; The function name sum without parentheses means that we are accessing this function pointer instead of calling the function. Be aware of this when passing a function as a value.
Understanding the function name is just a pointer, it can also be understood why there is no function overloading in ECMAScript, when we write two functions of the same name, just the pointer to the function name represented by the object that is written, The result of execution appears to be that the previous function was replaced by a function with the same name.
Before that, there are two ways to define a function, a function declaration, and a function expression. They work the same way when defining a function, but when the parser loads our JS code into the execution environment (such as a browser), the order in which the two function definitions are loaded is different.
The parser reads the function declaration first and makes it accessible before executing any code , and as for the function expression, it is interpreted and executed only when the parser executes the line of code it is in. For example:
alert (sum); can be executed//Although the function declaration is written after the function call functions sum (num1,num2) { return num1+num2;}
But the following is not possible:
alert (sum); Generates an error//function expression var sum = function (num1,num2) { return num1+num2;};
Since the function name itself is a variable in ECMAScript, the function can also be used as a value. That is, not only can you pass a function to another function like passing parameters, but you can return a function as the 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
Inside the function, there are two special objects: arguments and this.
The arguments object is similar to an array in that the bread has all the parameters passed into the function and can be accessed using the square bracket syntax of the array. The ECMAScript function does not mind passing in the number of arguments, and does not care about the type of arguments passed in, you can define a function to receive two parameters, but can be used to pass any parameter, or one does not pass. Because for the ECMAScript function, the named parameter is simply a convenience for us to use parameters in the body of the function, it is not necessary, and in the body of the function you can use the arguments object to get each parameter passed to the function.
In addition, the arguments object has a callee property, which is a pointer to the function that owns the arguments object.
another special object of the function is this, the behavior of this and the this type in Java. This refers to the environment object to which the function is executed. (When a function is called in the global scope, the This object refers to the global object of window.)
Window.color = "Red"; var o = {color: "Yellow"};function Saycolor () { alert (this.color);} Saycolor (); Redo.saycolor = Saycolor;o.saycolor (); Yellow
The value of this is not deterministic until the Saycolor function is called. Call Saycolor () in the global environment, the value of this is window. When O.saycolor () is called, this refers to the object o.
Properties of the function: length and prototype.
The length property is simple, which represents the number of named arguments the function expects to receive, that is, the number of parameters defined when the function is defined.
The prototype property, which is the prototype property of the function object, is introduced when the object is introduced. It's not unfolding here.
Method of the function:
Each function contains two non-inherited methods: Apply () and call ().
The purpose of both methods is to invoke the 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 of receiving the parameter is different, and it doesn't matter which one to use when the function is not passed.
Window.color = "Red"; var o = {color: "Yellow"};function Saycolor () { alert (this.color);} Saycolor.apply (o); Yellowsaycolor.call (o); Yellowsaycolor (); Redsaycolor.call (window); Red
JavaScript Reference type-function