Summarize js function knowledge points and js function knowledge points
In js, the reference type is a data structure, which contains five reference types: Object, data, Array, and re. Enter the code gExp and Function. Today we will talk about the Function data structure.
Js functions are actually objects, and each Function is a Function-type instance. Like other reference types, all attributes and methods are available.
1. There are several methods to define a function
1) syntax definition of function declaration
function sum(num1,num2){ return num1+num2; }
2) function expressions
Note that the function expression is followed by a semicolon;
var sum=function(num1,num2){ return num1+num2;}
Using Function Constructor
The constructor can receive any number of parameters, but the last parameter is always regarded as the function body, and the preceding parameter is
var sum=new Function("num1","num2","return num1+num2")
Of course, the third method is unfriendly and is not recommended.
The function name is just a pointer to a function, so the function name is no different from other variables that contain the object pointer. That is to say, a function may have multiple names.
What is the difference between function declaration and function expression?
When the parser loads data to the execution environment, the function declaration and function expression are different. The parser first reads the function declaration and allows the function declaration code to be called before any code. This is what we call the function declaration upgrade. Function expressions are different. They must wait until the function parser executes the code block to which the function is located before being actually executed. Compare the following two examples
Console. log (sum (10, 10); function sum (n1, n2) {return n1 + n2;} the above Code will be executed normally! Console. log (sum (10, 10); var sum = function (n1, n2) {return n1 + n2;} The above executor will report an error!
2. The function is not overloaded.
If two functions with the same name are declared, the result is that the subsequent functions overwrite the previous functions.
function sun(a){ return a+100;}function sun(a){ return a+200;}var result=sum(2)//202
3. Internal properties of the Function
Including this, arguments
Not described here
4. Function Attributes and Methods
The function has two attributes: length and prototype.
Length indicates the number of name parameters that the function wants to receive.
function a(name){ //todo}function b(name,age){ //todo}function c(){ //todo}a.length //1b.length //2c.length //0
The prototype attribute is very important for reference types. It will be explained in detail here.
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 to set the value of this in the function body.
First. The apply () method receives two parameters. One is the function running scope, and the other is the parameter array. The second parameter can be an array instance or arguments.
function sum(num1,num2){ return num1+num2;}function test1(a,b){ return sum.apply(this,arguments);}function test2(c,d){ return sum.apply(this.[c,d]);}console.log(test1(1,1)) //2console.log(test2(1,1)) //2
The call method is not much different from the apply method. The difference is that the second parameter of the call method must be passed to list the parameters one by one.
The biggest benefit of using call and apply to expand the scope is that the object is not coupled with any method.