


12 types of Javascript syntax that should not be used_Basic knowledge
Javascript has two sets of equality operators, one is == and !=, and the other is === and !==. The former only compares the equality of values, while the latter compares whether the types are the same in addition to values.
Please try not to use the former group, always only use === and !==. Because == performs type conversion by default, the rules are very difficult to remember. If you don't believe it, please answer whether the value of the following five judgments is true or false:
false == 'false'
false == undefined
false == null
null == undefined
0 == ''
The first three are false and the last two are true.
2.with
The original intention of with is to reduce keyboard input. For example,
obj.a = obj.b;
obj.c = obj.d;
can be abbreviated as
with(obj) {
a = b;
c = d;
}
But , during actual operation, the interpreter will first determine whether obj.b and obj.d exist. If they do not exist, then determine whether the global variables b and d exist. This leads to inefficiency and may lead to accidents, so it is best not to use the with statement.
3.eval
eval is used to directly execute a string. This statement should also not be used as it has performance and security issues and makes the code harder to read.
What eval can do, you can do without it. For example
eval("myValue = myObject." myKey ";");
can be written directly as
myValue = myObject[myKey];
As for the json string returned by the ajax operation, you can use the official website The parser provided by json_parse.js is run.
4.continue
The function of this command is to return to the head of the loop, but the loop will return to the head. Therefore, through proper construction, the use of this command can be completely avoided, improving efficiency.
5.switch The case statements in the
switch structure are executed sequentially by default, unless break, return and throw are encountered. Some programmers like to take advantage of this feature, such as
switch( n) {
case 1:
case 2:
break;
}
Writing like this is error-prone and difficult to find. Therefore, it is recommended to avoid switch penetration and add break wherever there is a case.
switch(n) {
case 1:
break;
case 2:
break;
}
6. Single-line block structure
if, while, do and for are all block structure statements, but they can also accept single-line commands. For example,
if (ok) t = true;
is even written as
if (ok)
t = true;
This is not conducive to reading the code, and it is very easy to make mistakes when adding statements in the future. It is recommended that curly brackets be added regardless of whether there is only one line of command.
if (ok){
t = true;
}
7. And --
increment operator and decrement operator--, directly Coming from the C language, it can make the code very compact on the surface, but in fact it makes the code look more complex and obscure. Therefore, for the sake of cleanliness and readability of the code, it is better not to use it.
8. Bitwise Operators
Javascript fully applies Java’s bitwise operators, including bitwise AND&, bitwise OR|, bitwise XOR^, and bitwise NOT. ~, left shift <<, signed right shift >>, and zero-padded right shift>>>.
This set of operators is for integers, so it is completely useless for Javascript, because inside Javascript, all numbers are saved as double-precision floating point numbers. If they are used, Javascript has to convert the operands to integers first and then perform the operation, which slows down the speed. Moreover, the "bitwise AND operator" & is the same as the "logical AND operator" &&, which is easy to confuse.
9. function statement
Define a function in Javascript. There are two ways to write it:
Function foo() { }
and
var foo = function () { }
The two writing methods are completely equivalent. However, during parsing, the former way of writing will be automatically promoted to the head of the code by the parser, thus violating the requirement that functions should be defined first and then used. Therefore, it is recommended to use the latter way of writing when defining functions.
10. Packaging objects of basic data types
The basic data types of Javascript include strings, numbers, and Boolean values, and they all have corresponding packaging objects String, Number, and Boolean. So, someone would define the relevant value like this:
new String( "Hello World");
new Number(2000);
new Boolean(false);
Writing like this is completely unnecessary and very confusing, so it is recommended not to use it.
In addition, the use of new Object and new Array is not recommended and can be replaced by {} and [].
11.new statement
Javascript is the first widely used language in the world to support Lambda functions. It is essentially a functional programming language similar to Lisp. But in the current world, more than 90% of programmers use object-oriented programming. In order to get closer to the mainstream, Javascript made a compromise and adopted the concept of classes, allowing objects to be generated based on classes. The
class is defined like this:
var Cat = function (name) {
this.name = name; new Cat('mimi');
This syntax of using functions to generate classes and new to generate objects is actually very strange and not intuitive at all. Moreover, when using it, it is easy to forget to add new, and it will become an execution function, and then there will be several more global variables inexplicably. Therefore, it is recommended not to create objects this way, but to use a workaround.
Douglas Crockford gave a function:
Copy code
Use this function when creating an object to operate on the prototype object:
After the object is generated, you can assign related attributes by yourself:
myCat.name = 'mimi';
12.void
In most languages, void is A type that represents no value. But in Javascript, void is an operator that accepts an operand and returns undefined.
void 0; // undefined
This command is useless and confusing. It is recommended to avoid using it.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











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.

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.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

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 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.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.
