The space-time and identity attributes of JavaScript Nested Function

Source: Internet
Author: User

JavaScript's function is not just a class citizen, it is a special citizen. It has a number of unique features:

1) It is an object that can store, pass, and attach properties.

2) It can have lexical closure, is a handy tool for event handling, and OOP encapsulation.

3) It can be anonymous and then called by the variable name or property attached to an object

4) It has a variety of "defined" ways: you can use Function statement, function expression, new, function object, and so on.

5) Special can be "bound" (via bind or apply) to a different object, with a different this pointer.

The flexibility and dynamism of JavaScript function makes it more functional and more dynamic and powerful than most languages known as functional programming language.

But sometimes when it comes to using some of its properties, it inevitably falls into subtle performance or functional problems. This article will give a small example:

JavaScript nested function, this property is, you can use a function definition as a parent function (or an outer function) of the statement (statement), it is best to illustrate the example:

function outerfunc (y) {

var internal1 = 100;

(nested) Function statements:

Innerfunc is in inner scope and can capture all outer variables

function Innerfunc(x) {

Internal1 = Internal1 + x;

return INTERNAL4 + x;

}

Innerfunc (y);

Console.log (' Internal1 is ': INTERNAL1);

return Innerfunc;

}

var ifun = outerfunc (1);

Ifun (2);

var ifun2 = outerfunc (3);

if (ifun = = = ifun2)

Console.log (' Ifun is equal to ifun2 '); Line1

Else

Console.log (' Ifun is not equal to ifun2 '); Line2

Innerfunc (2); Error: Innerfunc not defined

In the example above,Innerfunc is defined in Outerfunc , which defines Innerfunc as the "executable command",Innerfunc called "nested  Function Statement ". It has two characteristics worth noting:

    • Nested function statement is dynamically defined in the running peripheral functions (outer), resulting from.

That is to say, it is not only limited to peripheral functions on the SCOP, but also restricts the time of creation to run with peripheral functions, which is the " spatiotemporal attribute " of the nested function.

    • Nested functions and auto variables are similar, and outer function generates a new one every call.

This is the identity attribute of the nested function.

Know the 1th is very useful, this can have many use cases, know the 2nd, you can guess the end of the above program is to execute Line1 or Line2 it?

The answer is Line2, which calls Outerfunc Every time, produces a whole new innerfunc, something that the programmers who focus on performance need to know.

2014-8-20 Seattle

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.