问题描述
我O''Reilly的第90页Javascript权威指南第3版
有一个If / Else结构的例子:(删除了一些文字)
If(用户名!= null)
alert(Hello+用户名);
else {
username = prompt("你叫什么名字?);
alert(" Hello" + username);
}
我应该删除第一个" ;;"从第二行开始?
在我的测试中肯定会有更好的效果,但是我不知道这是否是书中的错误
,改变JavaScript的工作方式,或者浏览器中的一个缺陷
。
-
Steve Swift
On page 90 of my O''Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)
If (username != null)
alert("Hello " + username);
else {
username = prompt("What is your name?");
alert("Hello " + username);
}
Should I erase the first ";" from the second line?
It certainly works better in my tests that way, but I don''t know if this
is an error in the book, a change in the way JavaScript works, or a flaw
in my browser.
--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
推荐答案
编号为什么要这样?
这是命令的结束,为清楚起见,请使用分号。
No. Why should you?
It is the end of a command, and for clarities sake, use a semicolon.
如果没有分号,它的效果如何?
Erwin Moller
-
============================
欧文Moller
现在放弃googlegroups的所有帖子。
为什么?
= ===========================
How does it work better without the semicolon???
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
我用花括号包裹警告:
If(username!= null ){
alert(Hello+用户名);
} else {
username = prompt("你叫什么名字) ?");
alert(" Hello" + username);
}
-
Jorge。
I''d wrap that alert in curly braces:
If (username != null) {
alert("Hello " + username);
} else {
username = prompt("What is your name?");
alert("Hello " + username);
}
--
Jorge.
no ...:if(blah)
(小写)
no ... : if ( blah )
(lowercase)
行尾的'';''在JS中是可选的
函数hello(){
if(typeof username ==''undefined''|| username == null)
username = prompt(''你的名字是什么?'')
alert (''你好''+用户名)
}
的工作原理如下:
函数你好( ){
if(typeof username ==''undefined''|| username == null){
username = prompt(''你叫什么名字?'' );
}
alert(''Hello''+用户名);
}
但是......正确编码要好得多。
-
sm
The '';'' in end of line is optional in JS
function hello() {
if(typeof username == ''undefined'' || username == null)
username = prompt(''What is your name?'')
alert(''Hello '' + username)
}
works as well as :
function hello() {
if(typeof username == ''undefined'' || username == null) {
username = prompt(''What is your name?'');
}
alert(''Hello '' + username);
}
But ... it''s much better to code correctly.
--
sm
这篇关于如果是其他格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!