admin管理员组文章数量:1392002
If a
is undefined, this works:
if(window.a) {}
while this throws an error:
if(a)
Can someone explain why?
If a
is undefined, this works:
if(window.a) {}
while this throws an error:
if(a)
Can someone explain why?
Share Improve this question edited Apr 13, 2011 at 16:40 Pops 30.9k37 gold badges137 silver badges153 bronze badges asked Mar 30, 2010 at 8:16 Andreas KöberleAndreas Köberle 111k58 gold badges280 silver badges307 bronze badges 01 Answer
Reset to default 10window.a
is a property of window
and it's undefined. a
is a variable, and it's undeclared.
To use a variable, you should first declare it using the var
statement. Since you didn't declare a
, the interpreter raises an error. Object properties are not needed to be explicitly declared in order to use them. Crockford writes in The Good Parts:
If you attempt to extract a value from an object, and if the object does not have a member with that name, it returns the undefined value instead.
本文标签: What39s the difference between undefined and windowundefined in JavaScriptStack Overflow
版权声明:本文标题:What's the difference between undefined and window.undefined in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735346a2622294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论