12 inappropriate Javascript syntax collations

  • 2020-03-26 23:05:04
  • OfStack

1. = =
Javascript has two sets of equality operators, one set is == and! =, the other group is === and! = =. The former only compares the equality of values, while the latter, in addition to the value, compares whether the type is the same.
Please try not to use the previous group, always only use === and! = =. Because == is cast by default, the rules are hard to remember. If you don't believe me, please answer the following five questions for true or false:
False = = 'false'
False = = is undefined
False = = null
Null = = is undefined
0 = = ' '
The first three are false and the last two are true.

2. With
The idea of with is to reduce typing. Such as
Obj. A = obj. B;
Obj. C = obj. D;
I could just write it as
  with(obj) { 
    a = b; 
    c = d; 

In practice, however, the interpreter first determines whether obj.b and obj.d exist, and if they do not, then whether the global variables b and d exist. This leads to inefficiencies and can lead to surprises, so it is best not to use the with statement.

3. The eval
Eval is used to execute a string directly. This statement should also not be used because it has performance and security issues and makes the code harder to read.
What eval can do, it can do without it. Such as
Eval ("myValue = myObject." + myKey + ";" );
I can just write it as
MyValue = myObject [myKey];
As for the json string returned by the ajax operation, you can run it using the parser json_parse.js provided on the official website.

4. Continue
This command returns to the head of the loop, but the loop returns to the head anyway. So with proper construction, this command can be avoided altogether, resulting in improved efficiency.

5. Throughout the switch
Case statements in the switch structure, which are executed sequentially by default, unless a break, return, and throw are encountered. Some programmers like to take advantage of this feature, for example
  switch(n) { 
    case 1: 
    case 2: 

This is error-prone and hard to find. Therefore, it is recommended to avoid switch penetration and add break wherever there is a case.
  switch(n) { 
    case 1: 
    case 2: 

6. Single-row block structure
If, while, do, and for are block-structured statements, but you can also accept single-line commands. Such as
If (ok) t = true;
Even as
If (ok)
T = true;
This is not conducive to reading the code and is very error-prone when adding statements in the future. It is recommended that braces be used regardless of whether there is only one line of command.
{if (ok)
T = true;

7. + + and -
The increment operator ++ and the decrement operator --, which come directly from the C language, on the surface make the code very compact, but in fact make the code look more complex and obscure. So for the sake of code cleanliness and readability, don't use it.

8. Bit operator
Javascript fully applies Java's bitwise operators, including bitwise and &, bitwise or |, bitwise or ^, bitwise non-~, left shift < < , right shift with sign > > And right shift with 0 complement > > > .
This set of operators is for integers, so it's completely useless for Javascript, because inside Javascript, all Numbers are saved as double-precision floating point Numbers. Using them, Javascript has to convert the operands to integers and then perform the operations, which slows things down. And "bitwise and operator" & is easily confused with "logical and operator" &&.

9. The function statement
There are two ways to define a function in Javascript:
The function foo () {}
Var foo = function () {}
These are completely equivalent. However, when parsing, the former method is automatically promoted to the head of the code by the parser, which violates the requirement that functions should be defined before being used. Therefore, it is recommended to use the latter method when defining functions.

10. Wrapper objects for basic data types
Javascript's basic data types include strings, Numbers, and booleans, all of which have corresponding wrapper objects String, Number, and Boolean. So, someone will define the correlation value like this:
  new String("Hello World"); 
  new Number(2000); 
  new Boolean(false); 

This is completely unnecessary and confusing, so it is not recommended.
In addition, new Object and new Array are not recommended and can be replaced with {} and [].

11. The new statement
Javascript is the world's first heavily used language that supports Lambda functions, essentially a functional programming language of the same kind as Lisp. But in today's world, more than 90% of programmers use object-oriented programming. To get closer to the mainstream, Javascript has compromised by adopting 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; 
    this.saying = 'meow' ; 

Then, regenerate into an object
Var myCat = new Cat(' Mimi ');
This syntax, which USES functions to generate classes and new to generate objects, is strange and not intuitive at all. Also, when you're using it, it's easy to forget to add new, which turns into an execution function and adds a few more global variables for no apparent reason. Therefore, it is recommended not to create objects this way, but to use a workaround.
Douglas Crockford gives a function:
  Object.beget = function (o) { 
    var F = function (o) {}; 
    F.prototype = o ; 
    return new F; 

When you create an object, you use this function to manipulate the prototype object:
  var Cat = { 
  var myCat = Object.beget(Cat); 

After the object is generated, relevant properties can be assigned to:
MyCat. Name = 'Mimi';

12. Void
In most languages, void is a type that means there is no value. But in Javascript, void is an operator, accepts an operand, and returns undefined.
Void 0; / / undefined
This command is not useful and confusing and is recommended to avoid.

Related articles: