Function type
function type, is undoubtedly a very important thing in JS.
1. This thing is an object first, which means it is a reference type. Statement: One hears is the object, is not very has one kind its base class is the object illusion sense, No,
It and object are 2 separate things. When you typeof a function, the return is Funciton not object
2. Each function is an instance of a functions object, and it has properties and methods as well as other reference objects. Because it is an object, the function name is a pointer to the function object
Syntax support for declaration of functions:
<script>
/Mode 1
function Fun (num1,num2) {return
num1+num2;
}
Mode 2
var fun=function (num1,num2) {return
num1+num2;
};
Mode 3
var fun=new Function ("N1", "N2", "return N1+n2");
</script>
Explanation: 3 Ways to complete a function of the declaration, but each has a different.
Mode 1 is understood as the declaration of a function, mode 2, Mode 3 is understood as a function expression. (Mode 3 is not recommended, because it causes parsing of 2 times code, first interpreting the regular ECMAScript code, explaining the incoming arguments, the
Writing, the argument can be N, but the last argument is considered a function body.
Why is different, mainly because the JS parser on the function declaration and function expression parsing different. The parser takes precedence over the function statement, and the JS engine automatically puts the function declaration at the top of the execution environment when it executes.
The function expression is different, and when executed into a function expression, it is actually interpreted. It's important to understand this!
Look at the code
<script>
Console.log (typeof fun);//"function"
Console.log (typeof fun2);//"Undefined"
Console.log (typeof Fun3); "Undefined"
function fun (n1,n2) {return
n1+n2;
}
var fun2=function (n1,n2) {return
n1+n2;
}
var fun3=new Function ("N1", "N2", "return n1+n2;");
</script>
3. Why is the function not overloaded?
This problem should be considered from the language characteristics of JS. As has been said in article 2nd, the function name is just a pointer to the function object. It is clear from the concept of the pointer.
See code example:
<script>
function Fun (n1) {return
n1+100;
}
function Fun (n1) {return
n1+200;
}
Console.log (Fun (1)),///201
The above wording should be
var fun=function (N1) {return
n1+100 after parsing;
}
Fun=function (N1) {return
n1+200;
}
The fun reference is covered by
Console.log (Fun (1));//201
</script>
Today is written to this, are writing basic concepts, I hope that this piece has a blind spot to help! There is any need to correct the place, I hope you domineering message.
The above mentioned is the entire content of this article, I hope you can enjoy.