12 kinds of inappropriate JavaScript grammar Collation _ Basics

Source: Internet
Author: User
Tags bitwise bitwise operators wrapper
1.==
JavaScript has two sets of equality operators, one for = = and!=, and the other for = = and!==. The former only compares the equality of values, which, in addition to the value, compares the same type.
Please try not to use the previous group, always using only = = = and!==. The rules are hard to remember because the = = default is type conversion. If you don't believe me, 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 last two are true.

2.with
With is intended to reduce keyboard input. Like what
OBJ.A = obj.b;
OBJ.C = OBJ.D;
Can be written by Jane
Copy Code code as follows:

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

However, in the actual runtime, the interpreter will first determine whether OBJ.B and OBJ.D exist, and if not, then judge whether global variables B and D exist. This leads to inefficiency and can cause surprises, so it is best not to use the WITH statement.

3.eval
Eval is used to execute a string directly. This statement should not be used because it has performance and security issues and makes the code more difficult to read.
The things that eval can do without it can be done. 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 provided by the official website Json_parse.js.

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, it is possible to avoid the use of this command through proper construction, thus improving efficiency.

5.switch Penetration
Case statements in the switch structure, by default, in sequential execution unless Break,return and throw are encountered. Some programmers like to take advantage of this feature, such as
Copy Code code as follows:

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

It's easy to make mistakes, and it's hard to find. Therefore, it is recommended to avoid switch through, where there are case, all add break.
Copy Code code as follows:

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

6. Block structure of single line
If, while, do, and for are 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 conducive to reading the code, and will be very error-prone when adding statements in the future. It is recommended that the braces be added regardless of whether there is only one line of commands.
if (OK) {
T = true;
}

7.++ and--
The increment operator + + and the decrement operator--which comes 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. Therefore, for the sake of the neatness and readability of the code, it is not necessary to be good.

8. Bitwise operators
JavaScript fully applies Java bitwise operators, including bitwise AND &, bitwise OR |, bitwise XOR or ^, bitwise non-, left-shifted <<, signed right-shift >>, and right-shift >>> with 0 complement.
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 you use them, JavaScript will have to turn the arithmetic into integers first and then do the math, which slows down the speed. and "Bitwise AND operator" & &&amp with "Logic and operators", it's easy to confuse.

9.function Statement
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 writing will be automatically promoted to the head of the code, so that the function should be defined before the use of the requirements, so the proposed definition of the function, all adopt the latter one.

10. Wrapper object of basic data type
The basic data types for JavaScript include strings, numbers, and Boolean values, all of which have corresponding wrapper objects string, number, and Boolean. So, someone would define the relevant values like this:
Copy Code code as follows:

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

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

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's. But in the current world, more than 90% of programmers use object-oriented programming. To get close to the mainstream, JavaScript compromises and incorporates the concept of classes to allow objects to be generated from classes.
Class is defined in this way:
Copy Code code as follows:

var Cat = function (name) {
THIS.name = name;
this.saying = ' Meow ';
}

Then, regenerate into an object
var mycat = new Cat (' Mimi ');
This kind of syntax, which uses functions to generate classes and use new to generate objects, is very strange and not intuitive at all. Moreover, when used, it is easy to forget to add new, it will become an executive function, and then inexplicably more than a few global variables. Therefore, it is not recommended to create objects like this, but to adopt a workaround.
Douglas Crockford gives a function:
Copy Code code as follows:

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

This function is used to manipulate the prototype object when the object is created:
Copy Code code as follows:

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

After the object is generated, you can assign values to the related properties:
Mycat.name = ' Mimi ';

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

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.