Excerpt from the advanced programming of JavaScript
Ps: Very good book, you can go directly to read, here is just record. Recommend a computer ebook website: the Leather Bookstore
Search information: Mdn,w3cschool,google
- the execution environment of an anonymous function is global , so this is pointing to the window
- function is called, and its active objects are automatically given two special variables: This and arguments
- Call (), apply (), bind () point to its specified object
This: How the function is called determines the value of this
----in global context
Point to window, point to undefined in "use strict" mode
---------------------------
In a word, this is determined by the run-time context , who is the runtime context, and this points to who
function A () { returnthis }
A function is in the global environment
A () //equivalent to the following WINDOW.A ()
So this is pointing to window
------------
var name= "the Window"; var object ={ "My object", function() { return function () { returnthis. Name; anonymous function, Closure };} ; Alert (Object.getnamefunc () ()); The Window
Object.getnamefunc () returns an anonymous function because the execution environment of an anonymous function is global , so this point points to the window
-------------------
var name= "the Window"; var object ={ "My object", function() {
var that =this returnfunction() { return to that . Name; anonymous function, Closure }; // The Object
var = This is the object in the environment, so this is a pointer to object, which is obvious, so that.name is also pointing to Object.name
----------------
var o = {prop:37}; function Independent () { returnthis = Independent; // Logs 37
Equivalent to
var o = {prop:37};
O.F = function () {
Return This.prop
}
Console.log (O.F ()); Logs 37
So obviously this is in O environment, so this is pointing to O
--------------------------
var name= "the Window"; Var object ={"name": "My Object"};console.log ( (object.getname=function() { the anonymous function assigns a value and executes, so point to window returnthis. Name; }) ()); // Console.log (Object.getname ()); // My Object
There is a C-pointer feeling, the brain is a direction of the arrow, want the other end of the arrow, you know this point to WHO
---------------------------------------------------------------------------------------------------------------
1 var name = "the Window"; 2 3 var object = {4 Name: "My Object", 5 getname:function() {6 return This}9 (object.getname = Object.getname) (); The Window
- The 9th line will appear in Chrome
Uncaught Typeerror:cannot Read Property ' GetName ' of undefined
and IE11 under no problem, why????
[Basic]javascript----This