JavaScript Learning Note Function expressions
Two ways to declare a function: 1, Function declaration 2, function expression
1. Function declaration
function functionname (arg0, arg1, arg2) {
function Body FireFox Safari, Chrome, opera all define a nonstandard Name property for the function to access the function name, which is always behind the functions keyword
Alert (Functionname.name)
}
/* For function declarations, there is an important feature, which is function declaration promotion (Functions declaration hoisting)
The function declaration is read before executing the code, which means that the function declaration can be placed after it is called
*/
For example
Sayhi ();
function Sayhi () {
Alert ("hi!")
}
2, function expression (create an anonymous function [lambda function], and assign it to functionname)
var functionname = function (arg0, arg1, arg2) {
function body
}
If you use a function expression to create a function, the function expression, like any other expression, must be assigned before use, or an error will be made. This must be distinguished from the function declaration creation function.
Never do this,
if (conditoin) {
function Sayhi () {
Alert ("hi!");
}
}else{
function Sayhi () {
Alert ("yo!");
}
}
Because this is an invalid syntax in ECMAScript. The JavaScript engine tries to fix the error, but the browser corrects the error in a different way. Most browsers will return a second declaration, ignoring condition; Firefox returns the first declaration when condtion is true.
The right approach
var Sayhi;
if (conditoin) {
Sayhi = function () {
Alert ("hi!");
};
}else{
Sayhi = function () {
Alert ("yo!");
};
}
Some pitfalls of recursion
Recursion is the case of a function that calls itself by name.
function factorial (num) {
if (num <= 1) {
return 1;
}else{
return num * factorial (num-1)
}
}
If we refer to the factorial function through a variable, the variable will point to the factorial function, and an error will occur if the factorial is accidentally set to null.
var anotherfactorial = factorial;
factorial = null;
Alert (Anotherfactorial (4));//Error
This problem can be solved by Arguments.callee, only in non-strict mode
function factorial (num) {
if (num <= 1) {
return 1;
} else {
Return num * Arguments.callee (NUM-1)
}
}
Reason, by using Arguments.callee instead of the function name, you can ensure that no matter how the function is called, there is no problem.
However, the strict mode will be error, there is a method of both ways, this time using a named function expression to achieve the same result
var factorial = (function f (num) {
if (num <= 1) {
} else {
Return num * F (num-1)
}
});
Closed Package
Take the comparison condition function as an example
function Createcomparisonfunction (PropertyName) {
return function (Object1, object2) {
var value1 = Object1[propertyname];
var value2 = Object2[propertyname];
if (value1 < value2) {
return-1;
}else if (value1 > value2) {
return 1;
}else{
return 0;
}
};
}
Inside the anonymous function, internal access to the parameters of the external function Createcomparisonfunction PropertyName
Even if the external
JavaScript Learning Notes