一致性 [英] consistency

查看:95
本文介绍了一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中我们有if then else,如果后面没有多个语句,我们不需要使用{}。例如:


如果()

;


工作正常,


if()

if()

;

else

;


但是如果我们想让其他人申请父母呢?


if()

if()

;

其他

;


这里我们看到每个之后只有一个JavaScript语句如果否则。但是我们处于这样的位置,即如果将JavaScript应用于其他孩子。我们需要这样做:


if(){

if()

;

}

其他

;


即使父母if只有一个JavaScript语句。这是不一致的不是吗?这不是一个问题,只是因为我不喜欢不一致,除非根据规则对基于不一致的

有一个有效的解释。 JavaSxript。在那儿?谢谢。


-

George Hester

__________________________________

解决方案

" George Hester" <他******** @ hotmail.com>写道:

if()
if()
;

;

但是如果我们希望其他地方适用于父母如果?

if()
if()
;
其他
;

在这里,我们看到每个后面只有一个JavaScript语句,如果
和其他。但是我们处于这样的位置:JavaScript会将其他内容应用于孩子。


正确。

请注意第一个;"没有终止if声明,但

该if的内部(然后是分支)声明声明。这将是

给出相同的结果来写

---

if()

if()< br $>
{}

其他

{}

---


问题(如果可以称之为)对于所有编程来说都很常见

语言可选" else"分支机构并没有明确的

end-if标记。这包括从C

语法派生的所有语言。

我们需要这样做:

if(){
if()
;
}



即使父母if只有一个JavaScript语句
如下。这是不一致的不是吗?




不是真的。将它与减法相比较。如果你写4-3,它会给1.

如果你写6-1,它会给5分。但是,如果你写6-4-3,那就不是

给5.您需要添加括号以显示您希望它如何
关联以获得6-(4-3)的结果,因为有两种方法

关联6-4-3,编译器选择其中一个。如果你想要另外一个

,你需要添加明确的分组(在表达式中,你用
使用括号)。


同样

if(expr1)if(expr2)stmt1 else stmt2

可以通过两种方式理解:

if(expr1){if (expr2)stmt1 else stmt2}



if(expr1){if(expr2)stmt1} else stmt2


编译器默认为第一个。如果你想要第二个,你有

来添加显式分组(带有语句,你使用大括号)。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡色彩:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


George Hester写道:

在JavaScript中我们有if if else,如果没有比我们做的一个语句更多的
不需要使用{}。例如:


< - snip - >

即使父母if后面只有一个JavaScript语句。
这是不一致不是吗?这不是问题,只是因为我不喜欢不一致,除非基于JavaSxript的规则有不一致的有效解释。在那儿?谢谢。




这种不一致来自JS强制执行某些想法,

以及浏览器实现它的方式。就个人而言,我*总是*把我的if和

其他'放在{},然后,没有不一致,没有疑问,没有机会

我放东西在添加和忘记添加{}。


-

兰迪

comp.lang.javascript常见问题 - < a rel =nofollowhref =http://jibbering.com/faqtarget =_ blank> http://jibbering.com/faq


2004年10月5日星期二18:18:33 -0400,Randy Webb写道:

..个人而言,我*总是*把我的if和
其他然后,{},{+ blockquote>


最近在另一个小组讨论了一个情况

在接受采访时有OP,要求他们对特别麻烦的代码进行解释。


一瞬间他们评论说他们会重新打好它

所以它是wa清楚。他们得到了这份工作。


小组希望听到申请人更关心

可读,可维护的代码,而不是解释奥术

采访示例中运算符优先级的详细信息。


-

Andrew Thompson
http://www.PhySci.org/codes/ Web& IT帮助
http://www.PhySci.org/ 开源软件套件
http://www.1point1C.org/ Science&技术
http://www.lensescapes.com/ 图像逃脱世俗的


In JavaScript we have the if then else which, if there is not more than one statement following we do not need to use {}. For example:

if ()
;

works fine and so does

if ()
if()
;
else
;

But what if we want the else to apply to the parent if?

if ()
if ()
;
else
;

Here we see that only one JavaScript statement follows after each if and else. But we are in a position that JavaScript is going to apply the else to the child if. We are required then to do this:

if (){
if ()
;
}
else
;

even though the parent if has only one JavaScript statement following. This is inconsistent isn''t it? It''s not a
problem it''s just that I don''t like inconsistency unless there is a valid explanation for the inconsistency based
on the rules of JavaSxript. Is there? Thanks.

--
George Hester
__________________________________

解决方案

"George Hester" <he********@hotmail.com> writes:

if ()
if()
;
else
;

But what if we want the else to apply to the parent if?

if ()
if ()
;
else
;

Here we see that only one JavaScript statement follows after each if
and else. But we are in a position that JavaScript is going to
apply the else to the child if.
Correct.
Notice that the first ";" doesn''t terminate the "if" statement, but
the inner (then branch) statement of that "if" statement. It would
give the same result to write
---
if ()
if ()
{}
else
{}
---

The problem (if one can call it that) is common to all programming
languages with optional "else" branches and without an explicit
"end-if" marker. That includes all languages derived from the C
syntax.
We are required then to do this:

if (){
if ()
;
}
else
;

even though the parent if has only one JavaScript statement
following. This is inconsistent isn''t it?



Not really. Compare it to subtraction. If you write 4-3, it gives 1.
If you write 6-1, it gives 5. However, if you write 6-4-3, it doesn''t
give 5. You need to add parentheses to show how you want it to
associate to get the result of 6-(4-3), because there are two ways to
associate 6-4-3, and the compiler picks one of them. If you want the
other one, you need to add explicit grouping (in an expression, you
use parentheses).

Likewise
if (expr1) if (expr2) stmt1 else stmt2
can be understood in two ways:
if (expr1) { if (expr2) stmt1 else stmt2 }
and
if (expr1) { if (expr2) stmt1 } else stmt2

The compiler defaults to the first. If you want the second, you have
to add explicit grouping (with statments, you use curly braces).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


George Hester wrote:

In JavaScript we have the if then else which, if there is not more
than one statement following we do not need to use {}. For example:
<--snip-->
even though the parent if has only one JavaScript statement following.
This is inconsistent isn''t it? It''s not a problem it''s just that I don''t
like inconsistency unless there is a valid explanation for the inconsistency
based on the rules of JavaSxript. Is there? Thanks.



The inconsistency comes from the lax way that JS enforces certain ideas,
and the way browsers implement it. Personally, I *always* put my if and
else''s in {}, then, there is no inconsistency, no doubts, no chance of
me putting something in to add and forgetting to add the {}.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq


On Tue, 05 Oct 2004 18:18:33 -0400, Randy Webb wrote:

..Personally, I *always* put my if and
else''s in {}, then, there is no inconsistency, no doubts, no chance of
me putting something in to add and forgetting to add the {}.



There was a situation discussed on another group recently
that had the OP in an interview, being asked for their
interpretation of a particularly obtuse piece of code.

After a split second they commented they''d rewirte it
so it was clear. They got the job.

The panel wanted to hear the applicant was more concerned with
readable, maintainable code, than interpreting the arcane
details of operator precedence in the interview example.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane


这篇关于一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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