English 中文(简体)
Javascript: 是否有理由“如果(!a) ?
原标题:Javascript: Is there a reason to do "if (!a || (a && a.a))"?

是否有特别理由:

if (!options || (options && options.booleanCondition))

Instead of:

if (!options || options.booleanCondition) 

I m no javascript guru, 因此,也许有特殊案例或理由说明该守则的作者如此写了。

最佳回答

i 看不到这样做的理由

if (!options || (options && options.booleanCondition))

页: 1

if (!options || options.booleanCondition) 

除核查各种选择是否真实外:事实上,如果<代码>options <>/code>不是第一种条件,即true<>/code>,而第二个条件则从未得到评价,因为它有 的条件。

......

问题回答

也许它使用一个炉子。 这是一个突出的例子,但原因可能是:

var o = {};
var i = 0;

o.__defineGetter__( options , function() {
    return (i++) % 2 === 0 ? {booleanCondition: true} : null;
});

o.options; // Object first time
o.options; // null second time

这意味着:

if (!o.options || (o.options && o.options.booleanCondition))

!o.options is false (negating an Object), but after that o.options null (falsy) so that theeck isompulsory.

如果选择.boolean Conditions能够改变,那么是。 如果选择是假的,那么情况是真实的。 如果选择是真实的,则是ool。 条件是假的,如果情况是假的。 如果选择是真实的,则是 b。 情况是真实的,情况是真实的。

UPDATE

实际上,我猜测需要的是,因为如果选择是真实的,那么你的简化将是(如果是真的)真实的,那么结果就会与(如果是真实的还是真实的)相同。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.