Home  >  Article  >  Web Front-end  >  JavaScript Advanced Programming (3rd Edition) Study Notes 5 JS Statements_Basic Knowledge

JavaScript Advanced Programming (3rd Edition) Study Notes 5 JS Statements_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 17:49:13887browse

砖瓦和水泥都有了,接下来该是砌墙了,在ECMAScript中,语句就是我们需要砌的墙了。语句也和操作符一样,对于有C背景的人来说再自然不过了,下面采用类似的形式整理一下语句的相关知识,重点突出一些ECMAScript中比较特别和个人认为比较有意思的地方,同样,没有强调的但比较基础的语句并非不重要,而是我认为你已经熟悉。

语句一览

语句 语法 简要描述
简单语句

 ;

语句以分号(;)结束,在不引起歧义的情况下也可以省略分号。

语句块

{}

使用大括号({})将一组语句放一起组成一个语句块,在ECMAScript中,有语句块,但没有语句块作用域。

if语句

if(condition){}

if(ocndition){}else{}

条件选择,在条件表达式中,会将结果隐式转换为Boolean类型。

建议每个分支都明确使用{},以避免维护时出错。

条件语句可以嵌套。

switch语句

switch(expression)

{

case value1:

  statement1;

  break;

case value2:

  statement2;

  break;

default:

  statement;

  break;

}

switch语句语法和C语言一致,不同的是,switch中的expression不限于整型。

1、在switch语句中,表达式不限于整型,可以是任意表达式。

2、在case后面的value中,可以是整型,也可以是其它类型,甚至可以是一个表达式,但是在比较的时候不会进行类型转换,也即是使用全等(===)进行匹配。

3、case分支中的break表示不再继续后面的匹配,如果省略了会继续执行下面的case语句。建议每个case都加上break,如果是利用这种继续执行的特性,也加上相应注释说明。

4、最后一个分支的break加不加效果相同,我自己的个人风格是加上保持一致性。

do-while语句

do{

  statement;

}while(expression);

先执行循环体,再进行条件判断,这种格式至少会执行一次循环。

条件判断也会有隐式转换。

while语句

while(expression)

{

  statement;

}

满足条件才执行循环体。如果一开始就不满足条件,则根本不会执行循环体。

for语句

for(initialization; expression; post-loop-expression){

  statement;

}

for语句在功能上和while是等价的。

执行顺序是,先执行初始化initialization,然后进行条件比较expression,如果满足条件,就执行循环体,执行完一次循环后,执行post-loop-expression部分,然后循环比较条件直至跳出整个循环。

for-in语句

for(property in expression){

  statement;

}

for循环的另一种形式,可以使用这种循环遍历对象的属性和对象原型链上的属性。

with语句

with(expression){

  statement;

}

将代码的作用域设置到一个特定的对象中。

label语句 label:statement; 给代码添加标签供其它语句使用。
break语句

break;

break label;

1、用在switch语句中,在找到匹配的case分支后,不继续执行下面的case语句。

2、用在循环语句中中断整个循环。

continue语句

continue;

continue label;

在循环语句中中断本次循环,执行下一次循环。

try语句

try{

}catch(e){

}finally{

}

将代码放在try块中,使得异常发生时能够做出相应的处理。

throw语句 throw e;  抛出异常。
debugger语句 debugger; 调试。
return语句

return;

return expression;

返回语句。在return之后没有返回时,返回undefined。

For statements, the description is as follows:

1. Regarding whether to add a semicolon terminator (;) to a statement, my opinion is to add it to every statement and don’t let the engine guess your program. However, yesterday I read an article that was exactly the opposite of my point of view, and it was quite reasonable. Although it did not change my point of view, it did broaden my horizons.

2. For the var statement, due to the statement hoisting phenomenon in ECMAScript, it is recommended to put all the variables used in a scope at the top and use one var statement to define multiple variables. This is easy to understand and less prone to errors. At present, many JS libraries also use this form. The following is the code taken from the beginning of jQuery:

Copy code The code is as follows:

var document = window.document,
navigator = window.navigator,
location = window.location;

3. Used Statement block ({}) can also be used to define object literals. In ECMAScript, there is no block-level scope.
4. For the four types of loop statements (do-while, while, for, for-in), since the for-in statement will search the object itself and its prototype every time it loops, the efficiency will be relatively low. Regarding the optimization of for loop statements:
Copy code The code is as follows:

// 1. General for loop
for(var i=0; i < arr.length; i ){
}
// 2. The above will recalculate the length of arr every time it loops. If arr is DOM operation will obviously affect the efficiency. You can improve it
for(var i=0,l=arr.length; i}
// 3. This way the whole The loop will only calculate the length once. If decrement is taken into consideration, it can be modified to
for(var i=arr.length; i>0; i--){
}
// 4. Above No intermediate variables are used and the length only needs to be calculated once. If you consider that the length is always a number not less than 0, and the Boolean value of 0 in JS is false, you can further modify it to
for(var i=arr. length; i ; i--){
}
// 5. Considering the possible impact of variable declaration promotion in JS, in order to eliminate hidden dangers, it is modified to
var i=arr.length;
for(; i ; i--){
}

5. Although the with statement sometimes provides shortcuts, it often leads to unpredictable results. It is recommended to use it sparingly. Don’t even use:
Copy the code The code is as follows:

//1. Use the with statement
with(obj){
a=b;
}
//2. Without using the with statement, it is equivalent to the case of 1
if(obj.a === undefined){
a = obj.b || b;
}else
{
obj.a = obj.b || b;
}
//3. Possible results
a = b;
a = obj.b;
obj.a = b;
obj.a = obj.b;

Part 1 is to use the with statement , the second part is the equivalent statement without using the with statement, and the third part is the final possible running result. If you only look at the with statement itself, it is not easy to understand what will happen when the program actually runs. In addition, when using the with statement to make modifications, there will be out-of-synchronization problems. See the following code:
Copy code Code As follows:

var obj = {
person:{
name:'linjisong'
}
};
with(obj.person){
obj.person = {
name:'oulinhai'
};
console.info(obj.person.name); //oulinhai
console.info(name); //linjisong
}

A desynchronization may occur inadvertently here.
6. Please pay attention when returning from the return statement:
Copy the code The code is as follows:

return
{
prop:'value';
}//Since the engine will automatically add a semicolon, undefined will actually be returned here

return {
prop:'value' ;
}//Return an object
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn