如何优化JS中的arif语句 [英] How optimized ar eif-statements in JS

查看:60
本文介绍了如何优化JS中的arif语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们公司的代码中,我看到以下内容。


if(someStuff!= null){

if(someStuff!="" ;){

doThatThing();

}

}


虽然它是完全正确和有效,我想重写它

如下。


if(someStuff!= null&& someStuff!=" ;")

doThatThing();


我知道有些语言会评估第一个条件

的连词和,如果失败,则结束

表示该声明不会被执行。我想在这种情况下也假设
,但是不希望显示

傲慢和知道更好的主义,我想检查一下更多

首先是经验丰富的程序员。


请问JS会评估整个konjunction,还是会有足够的智能停止作为第一部分条件

失败了吗?是否取决于所使用的平台?


-

问候

Konrad Viltersten

In the code at our company i see the following.

if (someStuff != null) {
if (someStuff != "") {
doThatThing ();
}
}

While it''s fully correct and valid, i''d like to rewrite it
as follows.

if (someStuff != null && someStuff != "")
doThatThing ();

I know that some languages will evaluate the first condition
of the conjunction and, provided that it fails, conclude
that the statement is not to be performed. I''d like to
assume so in this case as well, but wishing not to show
arrogance and know-better-ism, i''d like to check with more
experienced programmers first.

Will JS evaluate the whole konjunction or will it be
intelligent enough to stop as the first partial condition
fails? Is it depending on the platform used?

--
Regards
Konrad Viltersten

推荐答案

KVilterstenescribió:
K Viltersten escribió:

if(someStuff!= null&& someStuff!="")
if (someStuff != null && someStuff != "")



[...]

[...]


JS会评估整个konjunction还是

智能足以在第一部分条件下停止

失败?是否取决于所使用的平台?
Will JS evaluate the whole konjunction or will it be
intelligent enough to stop as the first partial condition
fails? Is it depending on the platform used?



一旦确定结果就会停止:

http://developer.mozilla.org/en/docs...ical_Operators

参见短路评估标题。


-

- http://alvaro.es - álvaroG。Vicario - 西班牙布尔戈斯

- Mi sitiosobreprogramaciónweb: http://bits.demogracia.com

- Mi web de humor al ba?oMaría: http://www.demogracia.com

-

It''ll stop as soon as it has a definitive result:

http://developer.mozilla.org/en/docs...ical_Operators

See the "Short-Circuit Evaluation" header.

--
-- http://alvaro.es - álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al ba?o María: http://www.demogracia.com
--


5月22日上午9:46,K Viltersten写道:
On May 22, 9:46 am, K Viltersten wrote:

在我们公司的代码中,我看到以下内容。 br />

if(someStuff!= null){

if(someStuff!=""){

doThatThing();

}


}


虽然它完全正确且有效,但我想重写它

如下。


if(someStuff!= null&& someStuff!="")

doThatThi ng();
In the code at our company i see the following.

if (someStuff != null) {
if (someStuff != "") {
doThatThing ();
}

}

While it''s fully correct and valid, i''d like to rewrite it
as follows.

if (someStuff != null && someStuff != "")
doThatThing ();



在编写javascript时,它被广泛认为是好的风格

从不在代码周围省略块语句的大括号

在 - if / else - 分支中执行。有条件地执行

单个语句是非常常见的,然后才意识到需要将一个

秒语句添加到该分支。你已经使用了支撑

所有你要做的就是添加声明,但是如果没有

括号,你经常会因为缩进而被愚弄添加
$ b在现有的缩进级别上的$ b语句并没有看到

只有一个语句将被有条件地执行,并且任何跟随它的
将被无条件执行。通常的结果是

令人困惑且难以追查错误。

It is very widely considered good style when writing javascript to
never omit the braces of a block statement from around the code
executed in - if/else - branches. It is extremely common to have a
single statement conditionally executed and then to realise that a
second statement will need to be added to that branch. With the braces
already in place all you do is add the statement, but without the
braces you often get fooled by the indentation into adding the
statement at the existing level of indentation and not seeing that
only one statement is going to conditionally executed and any that
follow it will be unconditionally executed. The usual result is a
confusing and hard to track down bug.


我知道有些语言会评估第一个

条件的结合,并且,如果它b / b $ b失败,则断定该声明不是执行
。在这种情况下,我想假设这么好,但是希望不要表现出傲慢和

知道更好的主义,我想检查一下更多

首先是经验丰富的程序员。


请问JS会评估整个konjunction还是将它b / b
足够聪明才能停止第一部分

条件失败了吗?是否取决于平台

使用?
I know that some languages will evaluate the first
condition of the conjunction and, provided that it
fails, conclude that the statement is not to be
performed. I''d like to assume so in this case as
well, but wishing not to show arrogance and
know-better-ism, i''d like to check with more
experienced programmers first.

Will JS evaluate the whole konjunction or will it
be intelligent enough to stop as the first partial
condition fails? Is it depending on the platform
used?



语言规范要求短路,并且没有观察到

实现未能正确实现

这方面的规格。


然而,你的简化看起来似乎仍然不足以获得最佳结果。对于null和undefined值,第一个测试 - someStuff!= null - 将为

false,因此第一个测试将排除这两个值

。第二个测试, - someStuff!="" - ,对于布尔值false,数字零和空字符串,将为false

。所以

可以通过组合测试的值都是[* 1]个对象(包括函数),

非零数字(包括NaN),非空字符串,布尔值为true。


[* 1]实际上并非所有对象都会通过 - someStuff!="" - 测试

,因为与字符串的比较意味着类型转换所以带有 - toString的

对象 - 返回空字符串的方法将

等于空字符串原语。那是 -

alert({toString:function(){return'''';}} ==''''); - 警告真实。


另一种测试可能是: -


if(someStuff){

doThatThing();

}


- 其中 - someStruff的值 - 隐式类型转换为

boolean and因为空字符串,布尔值为false,数字零和

NaN,未定义的值和null所有类型转换为布尔值假值

该测试与您的唯一实际差异之前的一个

是NaN数值不会通过测试(这可能是一个好主意),并且所有对象都会通过而不管

他们如何键入 - 转换为原始值。虽然类型转换

到布尔测试比原始更短更简单(

包括至少一个隐式类型转换为布尔值)。

The language specification requires short circuiting, and no
implementations have been observed to fail to correctly implement the
specification in this regard.

However, your simplification looks like it probably still falls short
of an optimum outcome. The first test, - someStuff != null -, will be
false for null and undefined values, so those two values are excluded
by the first test. The second test, - someStuff != "" -, will be false
for boolean false, numeric zero and empty strings. So the values that
can pass the combined test are all[*1] objects (including functions),
non-zero numbers (including NaN), non-empty strings, and boolean true.

[*1] Actually not all objects will pass the - someStuff != "" - test
because the comparison with a string implies type-conversion so an
object with a - toString - method that returned the empty string would
be equal to the empty string primitive. That is -
alert({toString:function(){return '''';}} == ''''); - alerts "true".

An alternative test could be:-

if(someStuff){
doThatThing ();
}

- where the value of - someStruff - is implicitly type-converted to
boolean and since the empty string, boolean false, numeric zero and
NaN, the undefined value and null all type-convert to boolean false
the only practical differences between that test and your previous one
is that the NaN numeric value will not pass the test (which is
probably a good idea) and that all objects would pass regardless of
how they type-convert to primitive values. While the type-converting
to boolean test is shorter simpler and faster than the original (which
includes at least one implicit type-conversion to boolean anyway).


>在我们公司的代码中,我看到以下内容。
>In the code at our company i see the following.

>>
if(someStuff!= null){
if(someStuff!=") ;")这是完全正确有效的,我想重写它如下( doThatThing();
>>
if (someStuff != null) {
if (someStuff != "") {
doThatThing ();
}
}

While it''s fully correct and valid, i''d like to rewrite it
as follows.

if (someStuff != null && someStuff != "")
doThatThing ();



在编写javascript时,它被广泛认为是好的风格

从不在代码周围省略块语句的大括号

在 - if / else - 分支中执行。有条件地执行

单个语句是非常常见的,然后才意识到需要将一个

秒语句添加到该分支。你已经使用了支撑

所有你要做的就是添加声明,但是如果没有

括号,你经常会因为缩进而被愚弄添加
$ b在现有的缩进级别上的$ b语句并没有看到

只有一个语句将被有条件地执行,并且任何跟随它的
将被无条件执行。通常的结果是令人困惑并难以追踪错误。


It is very widely considered good style when writing javascript to
never omit the braces of a block statement from around the code
executed in - if/else - branches. It is extremely common to have a
single statement conditionally executed and then to realise that a
second statement will need to be added to that branch. With the braces
already in place all you do is add the statement, but without the
braces you often get fooled by the indentation into adding the
statement at the existing level of indentation and not seeing that
only one statement is going to conditionally executed and any that
follow it will be unconditionally executed. The usual result is a
confusing and hard to track down bug.



我忘记添加大括号,否则我总是按照copmany要求将它们放入

。当然,额外的指示和

建议总是受到赞赏。谢谢!


有可能陷入非常讨厌的事情 -

关于括号的论点当有需要时已经存在
$ b在我看来,$ b并不成立。


1.在某些时候需要将它们放入其中所以

如果频繁发生的话那个人需要在那里放更多的东西,这不是更多的工作。充其量,

不会少得多(或者更少,幸运的时候)。


2.除非一个人不是硬核 - 从未见过-anything-else

Python程序员,不应该混淆缩进和

范围。至少我从来没有遇到过那个问题,但也许我可以承认,这是一个天才。 :)


如果我错过了什么请赐教,请

请记住以上只是我的个人观点,

因此如有需要可能会有变化。

I forgot to add the braces, otherwise i''m always putting
them in by copmany requirement. The extra pointers and
suggestions are ALWAYS appreciated, of course. Thanks!

With risk of getting into something very nasty - the
argument about braces "already there when one needs them"
doesn''t hold, in my opinion.

1. At some point of time one needs to put them in so
if it happens frequently that one''ll need to put more
things there, it''s not MORE work to do so. At best,
not much less (or much less, when lucky).

2. Unless one isn''t a hard-core-never-seen-anything-else
Python programmer, one shouldn''t confuse indentation and
scoping. At least i''ve never had that problem but perhaps
i''m a genius, i can admit. :)

Please enlighten me if i''m missing anything and please
keep in mind that the above are only my personal views,
hence subject to change, if needed.


> JS会评估整个konjunction还是
当第一部分状态失败时,要足够聪明地停下来吗?它取决于使用的平台吗?
>Will JS evaluate the whole konjunction or will it
be intelligent enough to stop as the first partial
condition fails? Is it depending on the platform
used?



语言规范要求短路,并且没有观察到
实现未能正确实现

规范在这方面。


然而,你的简化看起来可能仍然没有最佳结果。

< snippage> ;

另一种测试可能是:


if(someStuff){doThatThing(); }


The language specification requires short circuiting, andno
implementations have been observed to fail to correctlyimplement the
specification in this regard.

However, your simplification looks like it probably stillfalls short of
an optimum outcome.
<snippage>
An alternative test could be:

if (someStuff) { doThatThing (); }



为了完全确定我做对了 - 我可以在原帖中跳过测试

并简单地测试is someStuff

案例?!这将是非常简单的!事实上,

可以让我欣赏JavaScript,呵呵。 :)


-

问候

Konrad Viltersten

To be perfectly sure i got it right - i can skip the test
in the original post and simply test "is someStuff the
case"?! That would be awsomely simplifying! In fact, that
could just make me appreciate JavaScript, hehe. :)

--
Regards
Konrad Viltersten


这篇关于如何优化JS中的arif语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆