12 inappropriate JavaScript syntax collation (GO)

Source: Internet
Author: User
Tags bitwise operators

1.==
JavaScript has two sets of equality operators, one set = = and! =, and the other is = = = and!==. The former compares only the values of equality, while the latter compares the same types in addition to the values.
Please try not to use the previous group, always only use = = = and!==. Because the = = type conversion is done by default, the rules are very difficult to remember. If you don't believe it, please answer the following five judgments whether the value is true or false:
False = = ' false '
false = = undefined
false = = NULL
Null = = undefined
0 = = "
The first three are false, and the latter two are true.

2.with
With is intended to reduce keyboard input. Like what
OBJ.A = obj.b;
OBJ.C = OBJ.D;
can be simply written

With (obj) {
A = b;
c = D;
}

However, when actually running, the interpreter will first determine if obj.b and OBJ.D exist, and if not, then determine if global variables B and D exist. This leads to inefficiencies and may cause surprises, so it is best not to use the WITH statement.

3.eval
Eval is used to execute a string directly. This statement is also not supposed to be used because it has performance and security issues and makes the code harder to read.
Eval can do things without it. Like what
Eval ("myvalue = myObject." + MyKey + ";");
can be written directly
MyValue = Myobject[mykey];
As for the JSON string returned by the Ajax operation, it can be run using the parser json_parse.js provided by the official website.

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, the use of this command can be avoided by proper construction, resulting in improved efficiency.

5.switch through
The case statements in the switch structure are executed sequentially by default, unless you encounter break,return and throw. Some programmers like to take advantage of this feature, such as

Switch (n) {
Case 1:
Case 2:
Break
}

This writing is error-prone and difficult to find. Therefore, it is recommended to avoid switch penetration, where there are case, all add break.

Switch (n) {
Case 1:
Break
Case 2:
Break
}

6. Single-line block structure
If, while, do, and for are both block structure statements, but you can also accept single-line commands. Like what
if (OK) T = true;
Even write
if (OK)
T = true;
This is not good for reading code, and it is very error-prone to add statements in the future. It is recommended that you add curly braces regardless of whether there is only one line of command.
if (OK) {
T = true;
}

7.++ and--
Incrementing the operator + + and decrement operators--directly from the C language--can make the code very compact on the surface, but it actually makes the code look more complex and obscure. For the sake of the cleanliness and legibility of the code, therefore not for good.

8. Bitwise operators
JavaScript fully applies Java bitwise operators, including bitwise AND &, bitwise OR |, bitwise XOR or ^, bitwise non ~, left shift <<, signed right shift >>, and right shift >>> with 0 topping.
This set of operators is for integers, so it's completely useless for JavaScript, because all numbers inside JavaScript are saved as double-precision floating-point numbers. If you use them, JavaScript will have to convert the operands to integers before doing the arithmetic, which slows down the speed. and "Bitwise AND Operator" & "Logical AND Operator" &&, it's easy to confuse.

9.function statements
There are two ways to define a function in javascript:
function foo () {}
And
var foo = function () {}
The two formulations are completely equivalent. However, when parsing, the previous method is automatically promoted to the head of the code, thus violating the function should be defined after the use of the requirements, it is recommended to define the function, all use the latter way of writing.

10. Wrapper objects for basic data types
JavaScript's basic data types include strings, numbers, and Booleans, and they all have corresponding wrapper objects string, number, and Boolean. So, someone would define the relevant value:

New String ("Hello World");
New Number (2000);
New Boolean (FALSE);

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

11.new statements
JavaScript is the first widely used language in the world to support lambda functions, and is essentially a functional programming language similar to Lisp. But in the current world, more than 90% of programmers are using object-oriented programming. In order to get close to the mainstream, JavaScript has compromised, adopting 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, regenerate into an object
var mycat = new Cat (' Mimi ');
This use of functions to generate classes, using new to generate the syntax of the object, in fact, is very strange, not intuitive. Moreover, when used, it is easy to forget to add new, it will become the execution function, and then inexplicably a few more global variables. Therefore, it is recommended that you do not create objects this way, but instead use a workaround.
Douglas Crockford gives a function:

Object.beget = function (o) {
var F = function (o) {};
F.prototype = O;
return new F;
};

This function is used to manipulate the prototype object as it is created:

var Cat = {
Name: ",
Saying: ' Meow '
};
var mycat = Object.beget (Cat);

After the object is generated, you can assign a value to the related property yourself:
Mycat.name = ' Mimi ';

12.void
In most languages, void is a type that represents no value. In JavaScript, however, Void is an operator that accepts an operand and returns undefined.
void 0; Undefined
This command is useless and confusing, and it is recommended to avoid it.

12 inappropriate JavaScript syntax collation (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.