Usage of this - Questions about this in Javascript. When a function is called as a method of an object, this refers to the superior object.
淡淡烟草味
淡淡烟草味 2017-06-14 10:54:15
0
4
677

I saw the second usage environment when the master introduced this. The original text is here:
http://www.ruanyifeng.com/blo...
The object o here should be this? If so why is this least congruent?

function test(){
    console.log(this.x);
  }
  var o = {};
  o.x = 1;
  o.m = test;
   console.log(o.m());
   console.log(o===this);

The output is:
1
false

淡淡烟草味
淡淡烟草味

reply all(4)
过去多啦不再A梦

This===window in global view

漂亮男人

When console.log() is executed in the global environment, this of course points to the window;
this points to the current execution environment of the function

洪涛

o.m() implicitly binds this to the o object
In the global scope, this points to the global object

仅有的幸福

Remember, there is another calling method func.call(context, x, m). The above two methods are just syntax sugar. You can use "conversion code" method such as:

function test(){
    console.log(this.x);
  } 

is equivalent to

function test(){
    console.log(this.x);
  }
test.call(undefined)

Logically speaking, the printed this should be undefined
But there is a rule in the browser:

If the context you pass is null or undefined, then the window object is the default context (the default context in strict mode is undefined)
So the this above should correspond to window.

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