本文介绍了当涉及对象字面量时,JavaScript是否提起if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var foo = {}; document.body.innerHTML = console.log = location.hash ='Hi'+'< br> '+ foo.bar +'< br> '+ foo.baz; setTimeout(function(){foo.baz = foo [bar] = []; foo.bar.push(new Date); foo.baz.push(new Date); document.body.innerHTML = console.log = location.hash ='Hi'+'< br>'+ foo.bar +'< br>'+ foo.baz},5000);
因为你设置了 node.bar
解决方案
等于 false
,(node.foo&&& node.bar)
将求值为 false
是否附加属性。不要检查这些属性是否 true
,您应该检查它们是否未定义:
if(typeof node.foo!=='undefined'&&&typeof node.bar!=='undefined')
{
...
}
else
{
...
}
var foo = {}; document.body.innerHTML = console.log = location.hash = 'Hi ' + '<br> ' + foo.bar + '<br> ' + foo.baz; setTimeout(function(){foo.baz = foo["bar"] = []; foo.bar.push(new Date); foo.baz.push(new Date); document.body.innerHTML = console.log = location.hash = 'Hi ' + '<br> ' + foo.bar + '<br> ' + foo.baz}, 5000);
解决方案
Since you set node.bar
equal to false
, (node.foo && node.bar)
will evaluate to false
whether the properties are attached or not. Rather than checking if those properties are true
, you should check whether they are undefined:
if (typeof node.foo !== 'undefined' && typeof node.bar !== 'undefined')
{
...
}
else
{
...
}
这篇关于当涉及对象字面量时,JavaScript是否提起if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!