


JavaScript Advanced Programming (3rd Edition) Study Notes 5 JS Statements_Basic Knowledge
砖瓦和水泥都有了,接下来该是砌墙了,在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:
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:
// 1. General for loop
for(var i=0; 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:
//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:
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:
return
{
prop:'value';
}//Since the engine will automatically add a semicolon, undefined will actually be returned here
return {
prop:'value' ;
}//Return an object

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor