Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果a未定义,则有效:
a
if(window.a) {}
虽然这会引发错误:
if(a)
有人可以解释为什么吗?
window.a是的一个属性,window它是undefined。a是一个变量,它是未声明的。
window.a
window
要使用变量,您应该首先使用var语句声明它。由于您没有声明a,解释器会引发错误。无需显式声明对象属性即可使用它们。Crockford 在The Good Parts中写道:
var
如果您尝试从对象中提取值,并且该对象没有具有该名称的成员,则它会返回未定义的值。