我在我们的应用程序中遇到了这段代码(已修改),但对它的工作原理感到困惑:
function someObject()
{
this.someProperty = {};
this.foo =
{
bar:
{
baz: function() { return "Huh?" }
}
};
this.getValue = function()
{
return (this.someProperty && this.foo.bar && this.foo.bar.baz && this.foo.bar.baz()) || null;
}
}
function test()
{
var o = new someObject();
var val = o.getValue();
alert(val);
}
当您调用test()函数时,将弹出文本“Huh?”。我不确定getValue的结果如何返回该值,我原以为执行A && B && C && D会返回true,而不是D的值。