简体   繁体   English

将属性设置为全局对象和窗口对象之间有什么区别?

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

this.f = function() {};
window.d = function() {};

d();
f();

Any difference? 有什么区别吗?

Not if it's run barely (eg not within special functions etc.). 如果它几乎没有运行(例如不在特殊功能内等),则不会。 Because then this === window . 因为那样this === window

In constructor functions etc. this has another meaning, so then it matters: 在构造函数等中, this具有另一种含义,因此很重要:

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

Now, 现在,

  • x() would set window.a to 123 x()会将window.a设置为123
  • var test = new x() would set test.a to 123 . var test = new x()会将test.a设置为123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM