所以我有这样的情况基本上错误,当Javascript'或'||在这种情况下使用运营商
var a = someUndefinedVariable.name || "";
在这种情况下,为什么“A”不等于空字符串??? 这个表达式抛出一个错误,所以我认为执行就停止了,Interpreter没有到达带有OR语句的部分。这样对吗? 但是,没有属性名称一切正常。 非常感谢您的关注。
所以我有这样的情况基本上错误,当Javascript'或'||在这种情况下使用运营商
var a = someUndefinedVariable.name || "";
在这种情况下,为什么“A”不等于空字符串??? 这个表达式抛出一个错误,所以我认为执行就停止了,Interpreter没有到达带有OR语句的部分。这样对吗? 但是,没有属性名称一切正常。 非常感谢您的关注。
您可能会检查someUndefinedVariable
第一,以防止一个未定义的变量
var a = someUndefinedVariable && someUndefinedVariable.name || "";
好的提示!谢谢! – Artdark92
由于someUndefinedVariable
的性质的访问,为undefined
,没有财产name
,所以它崩溃。其替换为:
var a = (someUndefinedVariable && someUndefinedVariable.name) || "";
This expression throws an error so I think execution just stops and Interpreter does not reaching to the part with the OR statement. Is it right?
是。异常停止执行(除非你是catch
他们)。
However, without property name everything works fine.
undefined
值,那么您可以自由访问它。这只是undefined
。undefined
值的属性会引发异常: 遗漏的类型错误:无法读取的不确定你正在处理的案件2和3
非常感谢您的回答。现在一切都很清楚! – Artdark92
你属性“栏”应该使用
var a = (someUndefinedVariable && someUndefinedVariable.name) || "";
因为您指的是未定义变量的属性,它会崩溃,而不是检查变量是否存在于h之前和
不错的提示!谢谢! – Artdark92
不可回答的问题。 –
@MarcoSalerno,我理解了它。 –
你猜对了,这是不同的 –