开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜