[英]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.