This article mainly introduces javascript function expressions in detail to help you understand javascript function expressions. if you are interested, refer to the following section to learn javascript function expressions.
1. create a function in the general form. the function declaration will be read before the code is executed. Therefore, you can write the function declaration below the function call:
sayHi(); function sayHi(){ alert("Hi!");}
2. create a function using a function expression. you must assign a value before calling it:
SayHi (); // error !! Var sayHi = function () {alert ("Hi! ");}
3. recursion
General recursion
function factorial(num){ if (num <= 1){ return 1; } else { return num * factorial(num-1); } }
Arguments. callee points to the pointer of the function being executed, which can be used for recursion:
function factorial(num){ if (num <= 1){ return 1; } else { return num * arguments.callee(num-1); } }
4. closure (Closure refers to a function that can access variables in another scope ).
Common method for creating a closure: create another function within a function. When a function is executed, an execution environment and corresponding scope chain are created. The closure can only obtain the last value of any variable in the function:
function createFunctions(){ var result = new Array(); for (var i=0; i < 10; i++){ result[i] = function(){ return i; }; } return result; } var funcs = createFunctions(); //every function outputs 10 for (var i=0; i < funcs.length; i++){ document.write(funcs[i]() + "
"); }
All the code output above is 10. This is because every funcs Function stores the activity object createFunctions () (this is a Function, an object, and a reference type Function), while createFunctions () this active object has a variable I, so every funcs will have this variable I, and when the createFunctions () function returns the result, I has changed to 10. Therefore, each value of the funcs array is 10.
It can be transformed as follows:
function createFunctions(){ var result = new Array(); for (var i=0; i < 10; i++){ result[i] = function(num){ return function(){ return num; }; }(i); } return result; }
When each anonymous function is called, the current value of I is given to num, and the closure of num is created and returned within the anonymous function. In this way, each function in the returned array has a copy of its own num variable. (This section is not clear, and the readers will try again. if there is a better description method, please comment below the article. thank you)
5. this object
- In global functions, this is equivalent to window.
- When a function is called as a method, this is equivalent to that object.
- When a function is called, this function automatically obtains two special variables: this and arguments. When searching for these two variables, internal functions only search for activity objects.
6. simulate block-level scope (private scope)
As follows:
function outputNumbers(count){ for (var i=0; i < count; i++){ alert(i); } alert(i); //count } outputNumbers(5);
In Java and other languages, the variable I in for is destroyed after it is used up. In javascript, when outputNumbers is called, the activity object is generated, and this I belongs to this activity object. Therefore, from its definition, it can be accessed everywhere inside the function, it is common within the activity object.
Syntax of anonymous functions (create a private scope ):
(Function () {// Here is the block-level scope })();
The function declaration is placed in parentheses, indicating that it is an expression, and you can call it immediately after adding a bracket.
If you need some variables temporarily, you can use the private scope:
function outputNumbers(count){ (function () { for (var i=0; i < count; i++){ alert(i); } })(); alert(i); //causes an error }
In the code above, I is private, and an error is reported when an anonymous function (private domain) accesses I externally, even though alert is still in the active object.
7. private variables
Function parameters, local variables, and other functions defined in the function are private variables of the function. For example:
function add(num1,num2){ var sum = num1 + num2; return sum; }
There are three private variables: num1, num2, and sum. Functions can access them internally, so they cannot be accessed externally.
The privileged method can access private variables: simply put, it uses expressions to give it a closure and access other functions within the closure:
function Person(name){ var a=0; this.getName = function(){ return name + a; }; this.setName = function (value) { name = value; }; }
This. getName and this. setName are expression methods. after creating a Person instance, you can only access the name and a attributes through getName or setName.
The above is all the content of this article, hoping to help you learn.