简体   繁体   English

为什么不调用未定义的属性tigger错误?

[英]Why doesn't calling an undefined property tigger error?

I wonder if 我怀疑是否

var myVar = unDef;

triggers an ReferenceError, why 触发ReferenceError,为什么

var myObj = {};
var myVar = myObj.unDef;

works fine? 工作正常? It returns undefined without a runtime error. 它返回undefined没有运行时错误。 Both aren't defined. 两者都没有定义。

This allows you to use 这使您可以使用

if (myVar.unDef)

rather than the more verbose (and more expensive, because it calls a function): 而不是更冗长(且更昂贵,因为它调用一个函数):

if (myVar.hasOwnProperty('unDef'))

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

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