12 kinds of Javascript syntax that should not be used
These days, I am reading “The Essentials of Javascript Language”.
This book is very thin, more than 100 pages, and it happens to be turned over during the holidays.
The author of the book is Douglas Crockford, he is currently one of the most proficient in Javascript in the world, and the creator of the Json format.
He thinks Javascript has a lot of dross. Because when Brendan Eich designed this language in 1995, it only took three months. Many language features were introduced to the market without careful consideration. As a result, by the time people realize these problems, there are already 1 million programmers using it, and it is impossible to significantly modify the language itself. Therefore, Douglas Crockford decided that he wanted to tell everyone which parts of Javascript are the essence and which parts are dross and chicken ribs.
The idea is very good, but I have to say that this book is not well written and is not suitable for novices to read. The reasons are as follows: 1) Douglas Crockford’s narrative is not clear, it is more like discussing issues with colleagues, rather than explaining the problem from the shallower to the deeper. The point of this book is not explanation, so after reading it, I think Javascript seems to have become more complicated. 2) He stubbornly uses a railroad diagram to explain every sentence. It seems that he is the only one in the world who uses this kind of picture which is more difficult to understand than Javascript. 3) The book is basically a simplified Javascript grammar manual, lacking enough new content. 4) There are too few examples in the book, and in the most difficult functions and objects, the examples used are all round-robin and progressive examples, which makes it very difficult to read.
The most valuable content of the book is not the main text, but the appendix. In Appendix B, Douglas Crockford lists 12 Javascript grammars that should be avoided, which I think are worth promoting.
===============================
1. ==
Javascript has two equal operators, one is == and !=, and the other is === and !==. The former only compares the equality of values, and the latter compares whether the types are the same in addition to the value.
Please try not to use the former group, and always use === and !==. Because == will perform 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;
}
However, in actual operation, the interpreter will first determine whether obj.b and obj.d exist, and if they do not exist, then determine whether the global variables b and d exist. This leads to inefficiency and may cause accidents, so it is best not to use the with statement.
3. eval
eval is used to directly execute a string. This statement should not be used, because it has performance and security issues and makes the code more difficult to read.
What eval can do can be done 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 parser json_parse.js provided by the official website to 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 structure, the use of this command can be avoided completely, so that the efficiency is improved.
5. Switch through
The case statement in the switch structure is 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;
}
This writing is prone to errors and hard to find. Therefore, it is recommended to avoid switch penetration and add break whenever 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-structured statements, but they can also accept single-line commands. for example
if (ok) t = true;
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 to add braces regardless of whether there is only one line of commands.
if (ok){
t = true;
}
7. ++ and –
The increment operator ++ and the decrement operator –, directly from the C language, can make the code very compact on the surface, but actually make the code look more complicated and obscure. Therefore, for the cleanliness and legibility of the code, it is not necessary.
8. Bitwise operators
Javascript completely applies Java’s bitwise operators, including bitwise AND &, bitwise OR |, bitwise XOR ^, bitwise not ~, left shift <<, signed right shift >> and right complemented with 0 Move >>>.
This set of operators is for integers, so it is completely useless for Javascript, because inside Javascript, all numbers are stored as double-precision floating-point numbers. If you use them, Javascript has to convert the operands to integers first, and then perform operations, which reduces the speed. And “bitwise AND operator” & is the same as “logical AND operator” &&, which is easy to confuse.
9. function statement
There are two ways to define a function in Javascript:
function foo() {}
with
var foo = function () {}
The two writing methods are completely equivalent. However, when parsing, the former way of writing will be automatically promoted to the head of the code by the parser, so it violates 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 string, number, and Boolean, and they all have corresponding packaging objects String, Number, and Boolean. So, someone will define the relevant value like this:
new String(“Hello World”);
new Number(2000);
new Boolean(false);
This writing is completely unnecessary and very difficult to understand, so it is not recommended to use it.
In addition, new Object and new Array are not recommended, you can use {} and [] instead.
11. New statement
Javascript is the world’s first widely used language that supports Lambda functions, and 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 from classes.
The class is defined like this:
var Cat = function (name) {
this.name = name;
this.saying =’meow’;
}
Then, generate another object
var myCat = new Cat(‘mimi’);
This kind of syntax of using functions to generate classes and using new to generate objects is actually very strange and not instinctive at all. Moreover, when using it, it is easy to forget to add new, it will become an execution function, and then some more global variables are inexplicably. Therefore, it is recommended not to create objects in this way, but to adopt a workaround.
Douglas Crockford gave a function:
Object.beget = function (o) {
var F = function (o) {};
F.prototype = o;
return new F;
};
When creating an object, use this function to operate on the prototype object:
var Cat = {
name:”,
saying:’meow’
};
var myCat = Object.beget(Cat);
After the object is generated, you can assign values to the relevant attributes yourself:
myCat.name =’mimi’;
12. void
In most languages, void is a type, meaning there is 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 it.
(over)