【发布时间】:2012-09-10 08:08:11
【问题描述】:
我无法理解在 Jquery/Javascript 中执行 || 的顺序。
如果我有这个:
someVar = $el.attr("one") || options.two || "three";
当$el.attr("one") 和options.two 都未定义时,它将someVar 设置为“三”。
我需要在此语句中添加另一个条件,如下所示:
someVar = $el.attr("one") || options.two || options.extra == "true" ? undefined : "three";
所以这应该说:
如果'$el.attr("one")'或'options.two'都没有定义,检查是否'options.extra == true',如果是,设置为'undefined',否则设置为'three'。
但是,即使我设置了$el.attr("one"),我总是得到undefined,但我不明白为什么?
谁能告诉我我的逻辑有什么问题?
谢谢!
【问题讨论】:
-
请注意,
options.extra == "true"仅在options.extra为假或恰好是字符串true时为真,这可能不是您想要的。
标签: javascript jquery operators operator-precedence