Discussion on the problem of function expression in JavaScript

Source: Internet
Author: User
Tags function definition

#函数表达式

# #函数声明和函数表达式的区别

There are two forms of function definition, one is function declaration, and one is function expression

When using declarations, pay attention to function declaration elevation, such as using declarations in an if statement to make an error, but there is no problem with the expression

The expression is to be defined before use, and the declaration is not

The declaration obtains a name property, and its name is empty in the expression

function fn () {}
var fn1 = function () {};
Console.log (Fn.name); Fn
Console.log (Fn1.name); //

# #递归调用

The following is a very common use of recursion for factorial:

function fn (num) {
if (num <= 1) {
return 1;
} else {
Return num * FN (NUM-1);
}
}
Console.log (FN (5))//120

If we assign this FN function to another variable, the variable will point to the function.

var a = new Object ();
A.say = fn;
Console.log (A.say (5)); 120

The contents of the function are still FN, and if you change this FN, something interesting will happen:

fn = function (num) {
if (num <= 1) {
return 1;
} else {
Return num * FN (num-2)
}
};
Console.log (A.say (5)); 40
Console.log (FN (5)); 15

Calling A.say () again after changing the FN is actually equivalent to invoking a different function within a function that defines the outer layer. Does not make the FN inside into a a.say.

We would think that it would be a waste to define this function all the time, and I would set it to Null,a.say not become empty because they are not pointers to the same function.

fn = null;
Console.log (A.say (5)); Error

However, the error occurs because the function fn inside the A.say method is null and longitude.

We want the A.say method to recursively call A.say itself instead of always FN, because it doesn't know later whether it will change back.
We can use the Arguments.callee.

function fn1 (num) {
if (num <= 1) {
return 1;
} else {
Return num * Arguments.callee (NUM-1);
}
}
var B = new Object ();
B.say = fn1;
FN1 = null;
Console.log (B.say (5)); 120

Look, so no matter how fn1 change, will not affect my B.say method again.

Be aware, however, that Arguments.callee cannot be accessed through scripting in strict mode. We can use the * * named function expression * *

var c = new Object ();
C.say = (function fn2 (num) {
if (num <= 1) {
return 1;
} else {
Return num * FN2 (NUM-1);
}
});
FN2 = null;
Console.log (C.say (5)); 120

In this way, even if the later fn2 is altered, C.say can also implement recursive invocation of itself.

# #闭包

Closure is strictly defined as "a collection of functions (environment) and their enclosing free variables" (node. JS Development Guide, Guo Jiabao)

Let's look at this interesting example first.

function fn () {
var result = [];
for (var i = 0; i <; i + +) {
Result[i] = function () {
return i;
}();
}
Console.log (result);
}
function fn1 () {
var result = [];
var myarr = [];
for (var i = 0; i <; i + +) {
Result[i] = function () {
return i;
};
}
for (var j = 0; J < J + +) {
Myarr.push (Result[j] ());
}
Console.log (Myarr);
}
FN (); [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
FN1 (); [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

To understand from a normal point of view:

In FN, each result member is given a value through a for loop;
In Fn1, for Loop, the member of each result is given a function. A value is obtained by calling this function. But why is the result different from the FN ()?

Because I in each result is derived from the function of its outer layer. FN, when the i=0 function is executed, so that its resulting I value is the current 0, and in Fn1, to result[j] function call, I is already 10, so it all the results are 10;

Now let's take a look at the problem of the scope chain and explain the above example from this perspective.

When a function is called * *, it occurs:

* An execution environment is created;
* The scope chain of the response is created, and the scope chain is saved from the internal properties of the function [[Scope]], and the scope chain is essentially a list of pointers to different ' variable objects '.
* The arguments and this value of the function are obtained.
* The values of arguments and other named parameters initialize the ' active object ' of the function

In a scope chain, the active object of its own function is ranked first, then the active object of the outer function, the outer layer, and finally the ' variable object ' of the global execution environment.
Accessing a variable in a function searches for a variable with the same name from a different variable object in the scope chain. The search is not out of the search.

The first FN above does not create a function inside, but executes a function directly and assigns its return value to result[i];

Discussion on the problem of function expression in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.