This explanation of JavaScript

Source: Internet
Author: User

Recently learning JavaScript, read "you Don T Know JS" This book, think is this JavaScript superior heart, have certain JavaScript basic friends must see (not recommended for children to see, afraid of being possessed). The author is knowledgeable, thorough, fluent, classic case, absolute great divine figure.

This article will be a summary of the book about this in JavaScript, but also add some of their own understanding. is to throw bricks, in order to lead Jade.

What exactly is this in JavaScript?

This is a marker that points to an object or undefined.

What is JavaScript? A programming language; What's the programming language doing? Write code, why write code? Execute the code and command the computer to handle things. Well, the written JavaScript code is used for execution, and the JavaScript engine specifies an execution environment (execution environment). The execution environment is complex and, in simple terms, can be understood as an execution data container (JavaScript can be called an object). JavaScript code (a series of statements) is executed in two places, the global area, and the function inside .

This is a property within the execution environment that points to an object to which the execution is directed.

In order to describe the relationship between this and the execution environment, it can be stated as follows:

Executor_environment = {invoke_stack:[statck1, Statck2, ...],params: [param1, Param2, ...],scope_chain:[scop1, scope2, ...],this:execute_ object,...}

Continue to cross-examine. What is the object of this point? When a function is called, this object is the context of the function execution, a target object that is dynamically bound by the JavaScript engine at run time .

4 scenarios for this in the function

There are 4 binding cases of this in the function, which are described below. The author of the original book emphasizes that in some scenarios there are four situations where there may be two or three of them, and the intensity of the utility is required, which I don't agree with, and I think it's possible to merge into one scenario.

New keyword

The new keyword in JavaScript is followed by a function call, and a new object is created, and when the function is executed, JavaScript engine assigns the newly created object to this, that is, this point points to the newly created object. Two examples are given below:

New Example 1:

function Func1 () {THIS.A = 3;} var a1 = new Func1 (); Console.log (a1.a); 3

New Example 2:

function Func2 () {this.a = 3;return {};} var a2 = new Func2 (); Console.log (a2.a); Undefined

The following example is to say that if the function has a return statement, then the new statement of the newly created object will be discarded !

Call and apply

The role of call and apply is to forcibly specify the object within the function that this should point to.

A small example of call:

function Sayhi () {Console.log ("Hi," + this.name);} var p = {name: "Kevin", Age:26};sayhi.call (p);
Called as a property of an object

There are two layers of meaning, function is a property of the object, access to the function through the object and immediate invocation. In this case, the this object inside the function is pointed to.

Example:

function Sayhi () {Console.log ("Hi," + this.name);} var p = {name: "Kevin", Age:26, Hi:sayhi};p. hi ();
Call function directly

The function is called directly by the function name, and JavaScript engine usually binds the global to this. Inside the browser, Global is window. However, it is said that the "use strict" case, this point to undefined (strictly speaking undefined in JavaScript is not the object).

Small example of function direct invocation:

function Sayhi () {Console.log ("Hi," + this.name); Console.log (this);} var name = "Just Joke"; Sayhi (); Output:hi, Just joke//followed global object, which contain name, Sayhi.
What does this represent in the global zone?

To tell the truth, I'm just guessing. The statement for the global zone also requires an execution environment, which also has a this identifier, which points to the global object. Inside the browser is window. This "you Don T Know JS" is not mentioned in the original book.

Global Zone Enter the following code to try:

var name = "James"; Console.log ("I am" + this.name); I am James
Reference links

You Don ' t Know JS

This explanation of 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.