国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

A puzzle about this in JavaScript
代言
代言 2017-06-26 10:52:59
0
7
1005

I have seen a lot of information saying that which object calls this function, this in this function points to this object.
In the following example, the function foo is called through the statement foo(). Why does this point to the global world? Isn't Window.foo() called by the global object?
Please advise, thank you!

var x = 10;
var obj = {
  x: 20,
  f: function () {
     var foo = function (){
         console.log(this.x);
     }
     foo();
  }
};
obj.f(); //10
代言
代言

reply all(7)
給我你的懷抱

There is a problem with what was said upstairs. Foo is not a global variable. A simple way to judge (non-strict mode) is:
1. When a function is not assigned a superior object, this points to window
2. When a function is assigned When it comes to superior objects, this only points to the closest superior (parent) object
such as foo.fn.o(), this in o points to fn

小葫蘆

This is what it looks like, I wrote it in the comments

var x = 10;
var obj = {
  x: 20,
  f: function () {
     var foo = function (){
         console.log(this.x);//你這是把函數(shù)賦值給一個(gè) foo的變量。此時(shí)的 foo 是全局的,所以下面調(diào)用 foo()這里是10嘛
     }
     foo();
  }
};
obj.f(); // 這個(gè)調(diào)用 f 函數(shù),因?yàn)?f(),并沒有返回出去什么,所以這里是 undefined
代言

For internal functions, that is, functions declared in the body of another function, they will be bound to the global object. This is a design flaw of JavaScript. The correct design method is that this of the internal function should be bound to the object corresponding to its outer function, thus causing the above problems.

In order to avoid this design flaw, you can use variable substitution. As a rule, you can use self or that. The code is as follows:

var x = 10;
var obj = {
  x: 20,
  f: function () {
     var self = this;
     var foo = function (){
         console.log(self.x);
     }
     foo();
  }
};
obj.f();
洪濤

First of all, let’s understand one thing:
1: window is also an object, it is a special object, it represents the whole world. When you call a function in the following way:
function foo(){....}
foo();//
This calling method in the second line (there is no object defined by you in front of the function), We call it 'global call'. In fact, it is equivalent to window.foo(). So did you see it? Calling a function globally is actually a special case of calling a function on an object, because the object at this time is window.
2: So why does the above code call foo() globally instead of on obj? I'll change the code and let it output 20:

var x = 10;
var obj = {
  x: 20,
  f: function () {
     console.log(this.x);
  }
};
obj.f();//20

Compare the two pieces of code and find their differences.

某草草
var x = 10;
var obj = {
  x: 20,
  f: function () {
     console.log(this.x);  // 20
     var foo = function (){
         // 這里函數(shù)的作用域是window
         console.log(this.x);
     }
     foo();
  }
};
obj.f(); //10
var x = 10;
var obj = {
  x: 20,
  f: function () {
     let that = this;
     var foo = function (){
         // 這里形成了閉包
         console.log(that.x);
     }
     foo();
  }
};
obj.f(); //20
曾經(jīng)蠟筆沒有小新

You can rewrite the code like this:

var x = 10;
var obj = {
  x: 20,
  f: function () {
     var foo = function (){
         console.log(this.x);
     }
     foo.call(null) // 等價(jià)于foo.call(window)
  }
};
obj.f.call(obj); //10  結(jié)果不變

Through the above example, you can understand that when calling a function, the JavaScript parser is called in the form of call or apply. In this way, specify a value for this in the function. The first parameter of these two methods is the internal this value of the foo method when it is called. If the first parameter of the call method is null or undefined, the global object will be used as the first parameter by default (you can try Try foo.call(), foo.call(null), foo.call(undefined))

女神的閨蜜愛上我

Function within function, this pointer is lost

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template