When I try to use an undeclared variable I get ReferenceError
:
console.log(a); // Uncaught ReferenceError: a is not defined
I could use a variable first and define it later and it won’t be a problem due to hoisting.
console.log(a); // undefined
var a;
But when I declare an object why would the execution context let me use any property of it?
var obj = {};
console.log(obj.a); // undefined
console.log(obj.why); // undefined
Why are these allowed even though a
and why
are never declared anywhere?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…