The language of JavaScript reading notes (iv)

Source: Internet
Author: User

This article summarizes the appendices to the book.

Appendix A: Dross

A.1 Global Variables

Global variables are the worst of all the bad features of JavaScript.

A global variable is a variable that is visible in all scopes. If some global variables happen to have the same name as the variables in the subroutine, they will conflict with each other and may cause the program to fail, and it is often difficult to debug.

There are 3 ways to define global variables:

(1) Arrange a var statement from any function:

var Foo=value;

(2) Add an attribute directly to the global object, and if it is a Web browser, the Global object name is window:

Window.foo=value;

(3) directly using an undeclared variable, called an implicit global variable:

Foo=value;

This way of implicitly declaring global variables makes it easy for beginners to make mistakes.

A.2 Scope

JavaScript does not have block-level scopes, only function scopes. So the way to declare a variable well is at the top of the function.

a.3 automatic insertion of semicolons

For example, a return statement returns a value where the starting part of the expression must be on the same line as return, otherwise it returns undefined.

return{foo:1};

The right way is:

return{foo:1};

a.4 reserved words

reserved words cannot be used to name variables or parameters. When used as a key value for an object literal, it must be enclosed in quotation marks .

var o={case:true};    illegal var o={' case ': true};    Correct o[' case ';      Correct o[case]; Error

a.5 Unicode

The JavaScript characters are 16-bit. Unicode treats a pair of characters as a single character, while JavaScript considers a pair of characters to be 2 different characters.

A.6 typeof

typeof cannot detect a null value:

typeof null== ' object ';

typeof the implementation of regular expressions is not consistent, some implementations are ' object ', some ' function '.

a.7 parseint

parseint stops parsing when it encounters a non-digit, such as:

parseint (' 12 ');    12parseInt (' top '); 12

You can see that the output is the same.

If the first bit of the string is 0, parseint is parsed in octal instead of decimal. So parseint (' 08 ') will return 0. This can cause problems when parsing the log or time. The workaround is to add one more parameter to the parseint:

parseint ("08", 10); 8

a.8 +

the + operator can do addition and string joins, depending on the type of the parameter.

When making additions, make sure that all parameters are integers.

a.9 floating point

binary floating-point numbers do not correctly handle decimal decimals, so 0.1+0.2 is not equal to 0.3. Fortunately, the integer operation of the floating-point number is accurate, so the problem with decimals can be avoided by specifying the precision.

a.10 NaN

Nan means that it is not a number, but the following expression is correct:

typeof NaN = = = ' number '; True

nan is not equal to himself.

Nan = = = Nan; False

the best way to determine whether a value can be used as a number is to use the Isfinite function, which will screen nan and infinity. You can define a isnumber function like this:

function Isnumber (value) {return typeof value = = = ' Number ' && isfinite (value);}

a.11 Pseudo-Array

JavaScript does not have a real array, and arrays do not produce out-of-bounds errors.

a.12 False Value


undefined and Nan are not constants, but global variables, and you can change their values. Don't do that.

a.13 hasownproperty (slightly)

? a.14 Object

?  JavaScript objects never use real empty objects, because they can get member elements from the prototype chain.

Appendix B: Chicken Ribs

? B.1 = =

= = and! = will be compared in the same two operand data types, otherwise it will be cast first.

' = = ' 0 ';    false0== ";    true0== ' 0 ';    truefalse== ' false ';    falsefalse== ' 0 ';    truefalse==undefined;    Falsefalse==null;    falsenull==undefined;    True ' \t\r\n ' ==0; True

as you can see, using = = is very confusing, so try to use = = = and!== comparisons.

B.2 with--don't use it!

B.3 eval--don't use it! (reduced security, difficult to read, low efficiency)

b.4 continue--efficiency is low, try not to use!

B.5 switch runs through--hard to detect errors, try not to use!

B.6 statement with missing block

if (OK) t=true; Advance ();

The actual expression is:

if (true) {t=true;} Advance ();

b.7 + +--

Try not to use it.

B. 8-bit operator--very low efficiency, do not use!

b.9 Function Statement comparison functions expression (slightly)

Wrapper objects of type b.10

Do not use new Boolean (), New Number (), New String ().

Avoid using new Object (), New Array (). You can use {} and [] instead.

b.11 New

The new operator is used to create a new object and bind the newly created object to the constructor's this object . Once you forget to use new, you get a call to a normal function, which is bound to the global object, which poses a big problem. And the compiler does not have an error or warning.

So the first letter of the constructor is capitalized, and a better way is not to use new.

b.12 void--This is an operator, return undefined, avoid using it!


The language of JavaScript reading notes (iv)

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.