Home > Web Front-end > JS Tutorial > body text

12 types of Javascript syntax that should not be used_Basic knowledge

WBOY
Release: 2016-05-16 17:17:40
Original
877 people have browsed it
1.==
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
Copy code The code is as follows:

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
Copy code The code is as follows:

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.
Copy code The code is as follows:

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:
Copy the code The code is as follows:

 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:
Copy the code The code is as follows:

 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

The code is as follows:  Object .beget = function (o) { var F = function (o) {}; F.prototype = o ; return new F;
};


Use this function when creating an object to operate on the prototype object:



Copy the code

The code is as follows: var Cat = { name:'', saying: 'meow' };
var myCat = Object.beget(Cat);


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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template