开发者

What is the difference between setting properties to the global object and the window object?

开发者 https://www.devze.com 2023-04-02 07:21 出处:网络
this.f = function() {}; wind开发者_StackOverflow中文版ow.d = function() {}; d(); f(); Any difference?Not if it\'s run barely (e.g. not within special functions etc.). Because then this === window.
this.f = function() {};
wind开发者_StackOverflow中文版ow.d = function() {};

d();
f();

Any difference?


Not if it's run barely (e.g. not within special functions etc.). Because then this === window.

In constructor functions etc. this has another meaning, so then it matters:

function x() {
    this.a = 123;
}

Now,

  • x() would set window.a to 123
  • var test = new x() would set test.a to 123.
0

精彩评论

暂无评论...
验证码 换一张
取 消