Cancer in the JavaScript language (UP)

Source: Internet
Author: User

JavaScript cancer in the language ( up )

recently turned over the JavaScript the essence of language, JS with a further understanding, especially JS the bad features, below, combined with the key points in the book, to share with you JS a few bad features .

Global variables

Global variables are variables that are accessible to all scopes, and in some small projects global variables give us the flexibility and convenience of acquiring and using functions, data, and so on, but as programs become larger, global variables become more difficult to maintain; (because a variable can be modified in many places and may be overwritten), It is difficult to locate and debug once a problem occurs.

Js the problem is not only that it allows the use of global variables, but also that it relies on global variables, JS does not have a linker in it, all the compilation units are loaded into a common global object;

There are generally three ways to define the use of global variables:

1, In addition to any function, define, for example: var name = "Jxj" ;

2, directly to the global object, add a property, such as: window.job = "FED";

3, using undeclared variables directly, this is called an implicit global variable, such as: name = "JXJ" ;

Given the problem of global variables, it is generally recommended to avoid using global variables and global pollution in two ways:

1, create a namespace, such as: var my sapce ={}

Myspace.login = {"ModelID": "Login", PWS: "* * *"};

MySpace.log = {"ModelID": "Log", Content: "* * *"};

2, use closures to encapsulate variables within their scope to avoid contamination of the global

Scope

Js The syntax comes from C , a block of code creates a scope, and the variable declared in fast code is not visible outside of it; JS This block syntax is used, but the block-level scope is not provided, and the variable declared in the code block is visible anywhere in the function that contains the code block;

In most languages, in general, the best place to declare a variable is where it was first used; JS This is not good because he does not have a block-level scope, so it is recommended to declare all variables at the beginning of each function.

Automatically inserting semicolons

Js There is an automatic repair mechanism that tries to fix the defective program by automatically inserting a semicolon, but never expect that he may cover up more serious mistakes.

Sometimes he would be inappropriate to insert a semicolon; if a return statement returns a value, the starting part of the value expression must be on the same line as return:

Return

{

Status:true

} ;

this appears to be to return a containing Status the object of the member element, but the automatic insertion of a semicolon causes it to return undefined , the automatic insertion of semicolons causes the program to be misunderstood, but without any hint, resulting in a problem difficult to locate;

The way to avoid this problem is to put the " {"put at the end of the previous line instead of at the beginning of the next line, such as:

return {

Status:true

};

Reserved words

Js can not be used to name variables or parameters, (each browser has different processing for different versions of the reserved word usage limits, depending on the test Object = {case:value}; It is legal in the current mainstream browser version, but it may be problematic in the old version, but when reserved words are treated as key values of the object literal, they must be enclosed in quotation marks, they cannot be used in point notation, so it is sometimes necessary to use parentheses notation.

So the best way is not to use reserved words as variable names.

parseint

parseint () is a function that converts a string into a certificate, and he encounters a non-string that stops parsing; parseint ("+ kg"), parseint ("+"), parseint ("tons") The result is the same; we might want to parse the error, not just stop parsing, it's better to tell us something went wrong, but it doesn't.

0 8,9 is not a number, so parseint ("the"), parseint ("the"); all produce 0 as a result; This error causes an error in parsing the date and time.

However, parseint () can accept a cardinality, as an argument, parseint ("The", "Ten"), we're back. 8 , so it is recommended to add this technical parameter.

Plus " + "

+ operators can be used for addition operations or string joins, as to exactly how they are performed, depending on the type of the parameter:

var ss = "" +12;

ss

"12"

var dd = "Jxj" +12;

dd

"Jxj12"

var cc =12+34;

46

This complex behavior is the program Bug common sources, so, use the + , we must be well considered comprehensive.

Floating point number

Two-level floating-point numbers do not correctly handle decimal decimals, so 0.1+0.2 Not equal to 0.3 ; this is JS The most frequently reported Bugs, and it is a deliberate result of following the binary floating-point arithmetic standard two; but we multiply by multiplying - , etc. to convert, divide by - and so on, get the accurate result;

NaN

NaN is a IEEE754 A special quantity value defined in the, he represents not a number, although the following expression returns true:

typeof NaN = "number"//true

This value generally occurs when the Apprentice converts a non-numeric string into a number, such as:

var AA = parseint ("AA");

Aa

NaN

General typeof cannot distinguish between numbers and NaN , and it does not equal itself, so the following code will surprise you:

NaN ===nan;

False

NaN!==nan;

True

JS provides a IsNaN function, you can distinguish between numbers and NaN, as follows:

IsNaN ("12");

False

IsNaN (NaN);

True

IsNaN ("Jxj");

True

IsNaN (12);

False

of course, the best way to determine whether a value can be used as a number is to use isfinite function, because it will filter out NaN and the Infinity;

Isfinite (12);

True

Isfinite ("12");

True

Isfinite ("Jxj");

False

Isfinite (NaN);

False

Isfinite (Infinity);

False

Unfortunately, it is not accurate to try to convert its operators into a number;

The following defines a Isnumber function:

var isnumber =function (value) {returntypeof value = = = "Number" && isfinite (value);}

Undefined

Isnumber (12);

True

Isnumber ("12");

False

Isnumber (NaN);

False

Isnumber (Infinity);

False

Isnumber ("Jxj");

False

To be continued .....


Cancer in the JavaScript language (UP)

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.