12 types of JavaScript syntax not appropriate for use

Source: Internet
Author: User
Tags bitwise bitwise operators

Nanyi

These days, I was reading the language of JavaScript.

This book is very thin, more than 100 pages, just over the holidays.

The author of the book is Douglas Crockford, one of the most JavaScript-savvy people in the world, and the creator of the JSON format.

He thinks JavaScript has a lot of dross. Since the 1995 Brendan Eich designed this language, it took only three months, and many of the language features were not well thought out and were pushed to the market. As a result, when people are aware of these problems, 1 million of programmers are already using it, and it is impossible to drastically modify the language itself. So, Douglas Crockford decided he wanted to tell you what parts of JavaScript are pristine and which parts are dross and chicken ribs.

This idea is very good, but I have to say that this book is not good enough for beginners to read. The reasons are as follows: 1) Douglas Crockford narrative is not clear, more like with peers to discuss issues, rather than the easy to explain the problem. The focus of this book is not to explain, so after reading it, I think JavaScript seems to be getting more complicated. 2) He stubbornly uses the railroad map (railroad diagram) to explain each statement. There seems to be only one person in the world who uses this figure that is more ugly than JavaScript. 3) The book is basically a simplified JavaScript syntax manual that lacks enough new content. 4) The book is too small, and in the most difficult function and object part, the use of examples are ring-ring, the layer of progressive examples, resulting in a difficult reading.

The most valuable content of the book is not the text, but the appendix. In Appendix B, Douglas Crockford lists 12 JavaScript syntax that should be avoided, which I think is worth promoting.

==============================

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 runs 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 [].

One. New statement

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 ';

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.

Finish

"Reprint" http://www.ruanyifeng.com/blog/2010/01/12_javascript_syntax_structures_you_should_not_use.html

12 types of JavaScript syntax not appropriate for use

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.