Talk about my understanding of JavaScript prototypes and closures (handy notes 6) _javascript tips

Source: Internet
Author: User
Tags closure eval prepare

Related readings: Talk about my understanding of JavaScript prototypes and closures (handy notes 8) I understand JavaScript prototypes and closures (handy notes 9)

What is closure

What's the closure? Closures are closure, which is a new feature that static languages do not have. But closures are not something that is too complex to be understood, in short, closures are:

• Closures are a set of local variables for functions, except that these local variables continue to exist after the function returns.

The closure is that the function "stack" is not released after the function returns, we can also understand that these function stacks are not allocated on the stack but are allocated on the heap

• When you define another function within a function, a closure is generated

What is a prototype?

A prototype is an object that other objects can use to implement property inheritance.

Can any object be a prototype?

Is

Which objects have prototypes

All objects have a prototype by default, because the prototype itself is also an object, so each prototype itself has a prototype (with one exception, the default object prototype is at the top of the prototype chain).

Execution context

Each time the controller goes to the ECMAScript executable code, it goes into an execution context.

The execution context (referred to as-EC) is an abstract concept that distinguishes the ECMA-262 code from the concept of executable (executable).

The standard specification does not accurately define the type and structure of the EC from the point of view of technology implementation; This should be a problem to consider when implementing the ECMAScript engine concretely.

The execution context of the activity logically constitutes a stack. The bottom of the stack is always the global context, and the top of the stack is the current (active) execution context. The stack is modified at the same time that the EC type variable (various KINGDS of EC) is pushed or ejected.

--------------------------------------------------------------------------------

situation One: in a section of JS code to come up with a real sentence before running, the browser has done some "preparatory work", which includes the declaration of variables, rather than assignment. Variable is assigned when the assignment statement executes.

situation Two: in the "Ready to work" phase, this is directly assigned.

Copy Code code as follows:

Console.log (This)//window

case Three: functions: Function expressions and Function declarations

 Console.log (F1); The function F1 () {}
function f1 () {}//function declaration
Console.log (F2);//undefined

"Prepare for Work" Summary:

• Variable, function expression ———— variable declaration, default assignment is undefined

This ———— assignment

• Function Declaration ———— Assignment

The preparation of these three kinds of data is called "execution context" or "execution context environment".

--------------------------------------------------------------------------------

JavaScript performs these "preparations" to build the execution context before executing a code snippet. This "code snippet" is divided into three cases-Global code, function code, eval code.

 Global Code Snippets
<script type= "Text/javascript" >
 //Code Snippets ...
</script>
//Function code Snippet function
fn (x) {
 console.log (x + 5);
}
var fn = new Function ("X", "Console.log (x + 5)");
Eval Code Snippet
eval (' var x = Ten ');
(function foo () {
 eval (' var y = = ');
}) ();
alert (x);
alert (y);//"Y" is not defined

--------------------------------------------------------------------------------

In a function, there will be other data in addition to the "Prepare for work" scenario

 function fn (x) {
 console.log (arguments);//[10]
 conosole.log (x);//10
}

The above code shows that both the arguments variable and the function argument have been assigned before the statement of the function body is executed.

Each time a function is invoked, a new context execution environment is generated. Because different calls can result in different parameters.

When the function is defined (not called), the scope of the free variable within the function body is determined.

 var a = ten;
function fn () {
 console.log (a);//a is a free variable
}     //function creation, it determines the scope function
Bar (f) {
 var a =
 ) to take a value. f (); Print "10" instead of "a"
}

Summarize:

The context environment data for global code is as follows:

• Generic variables (including function expressions), such as: var a = 10 | ===> declaration (default assignment undefined)

• Function declarations, such as functions fn () {} | ===> Assign Value

This | ===> Assign Value

function body

• Parameters | ===> Assign Value

Arguments | ===> Assign Value

• Scope of values for free variables | ====> Assign Value

A popular definition:

Before executing the code, all the variables that will be used are taken out in advance, some directly assigned, and some are undefined with the first digit.

The above is a small set to share the JavaScript prototype and closure of the series of understanding (handy note 6) of the entire narrative, I hope you like.

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.