Recently studied 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. Fear of being possessed).
The author is knowledgeable and thorough in understanding. Fluent, case-classic, Absolute big-god character.
This article will be a summary of the book about this in JavaScript, but also add some of their own understanding.
It's a brick toss. With a view to attracting 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 the code. Why write code? Run the code and command the computer to handle things. Well, the written JavaScript code is used to run, and JavaScript engine specifies a runtime environment (execution environment). The operating environment is very complex and simple. Can be understood as a running data container (JavaScript can be called an object). JavaScript code (a series of statements) is run in two places, the global area, and the function inside .
This is a property within the running environment that points to an object that is being targeted by the run .
In order to describe clearly the relationship between this and the operating environment, simply. Able to express for example the following:
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 in which the function executes and a target object. is dynamically bound by JavaScript engine at execution time .
4 scenarios for this in the function
There are 4 binding cases for this in the function. The following will be introduced one by one.
The author of the book emphasizes that in some scenarios, there are four situations where there may be two or three of them, and it is necessary to rank the intensity of the utility. I don't quite agree with that, and I think it's possible to merge into one scenario.
Newkeyword
JavaScript inside Newkeyword followed by a function call, will create a new object, when running function, JavaScript engine will assign the newly created object to this, that is, this time this point to the newly created object. Two examples are given below:
New Sample 1:
function Func1 () {THIS.A = 3;} var a1 = new Func1 (); Console.log (a1.a); 3
New Sample 2:
function Func2 () {this.a = 3;return {};} var a2 = new Func2 (); Console.log (a2.a); Undefined
The following example is to say, suppose the function has a return statement. The newly created object of the new statement is 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 sample 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
Here are two layers of meaning. A function is a property of an object that is called by an object to access the function and call it immediately . In this case, the this object inside the function is pointed to.
Examples:
function Sayhi () {Console.log ("Hi," + this.name);} var p = {name: "Kevin", Age:26, Hi:sayhi};p. hi ();
Call function directly
function is called directly by the function name. Usually JavaScript engine binds global to this. Inside the browser, Global is window. It is said that "use strict" in the case, this point to undefined (strictly speaking undefined in JavaScript is not an object).
Function direct Call Sample:
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?
Tell the truth. I was just pushing. The statement for the global zone also requires a running 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
References link
You Don ' t Know JS
This explanation of JavaScript