12 types of JavaScript syntax that are not suitable for use

Source: Internet
Author: User
Tags bitwise bitwise operators

1. = = (O゜▽゜) o☆[bingo!]

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 = = undefinedfalsenullnull = = undefined0  "'

The first three are false, and the latter two are true.

2. With (O゜▽゜) o☆[bingo!]

With is intended to reduce keyboard input. Like what

OBJ.A == OBJ.D;

can be simply written

== 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 (O゜▽゜) o☆[bingo!]

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. " " ; ");

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 (O゜▽゜) o☆[bingo!]

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 (O゜▽゜) o☆[bingo!]

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:  case2:  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) {  case1:  break;  Case2:  break;}

6. Single-line block structure (O゜▽゜) o☆[bingo!]

If, while, do, and for are both block structure statements, but you can also accept single-line commands. Like what

if true;

Even write

Iftrue;

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.

Iftrue; }

7. + + and-- (O゜▽゜) o☆[bingo!]

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 operator (O゜▽゜) o☆[bingo!]

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 Statement (O゜▽゜) o☆[bingo!]

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 object of the basic data type (O゜▽゜) o☆[bingo!]

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 ("HelloWorld"); New Number (a); 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 (O゜▽゜) o☆[bingo!]

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

Then, regenerate into an object

var 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 == o;   returnnew  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:

' Mimi ';

void (O゜▽゜) o☆[bingo!]

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.

Not to say no, but to use less (O゜▽゜) o☆[bingo!]

12 types of JavaScript syntax that are not suitable 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.