V8 and JavaScript gotchas

Source: Internet
Author: User

Gotcha #1: This keyword


Gotcha #2: variable scope and variable evaluation strategy

Object properties are not iterated in order (V8)

Comparing nan with anything (even Nan) is always false

Console. Log ("inputis 123 -",! Isnan (parseint ("123", 10 )));

Console. Log ("inputis ABC -",! Isnan (parseint ("ABC", 10 )));

 

Floating point Precision

Console. Log (0.1 + 0.2); // 0.30000000000000004

Console. Log (0.1 + 0.2 = 0.3); // false

 

Functions are objects, so they can have properties attached to them.

Function dosomething () {return dosomething. Value + 50 ;}
VaR dosomethingelse = function () {return dosomethingelse. Value + 100 ;};
Dosomething. value = 100;
Dosomethingelse. value = 100;
Console. Log (dosomething ());
Console. Log (dosomethingelse ());

  1. Logs 150
  2. Logs 200


The arguments variable available in functions is
Not an array, through it acts mostly like an array. For example, it does not have the push () and POP () methods but it does
Have a length property

 

To create an array from the arguments property, you can use array. Prototype combined with function. call:

 

Function Test (){

// Create a new array from the contents of arguments

VaR ARGs = array. Prototype. Slice. Call (arguments); // returns an array

Console. Log (ARGs. Length );

Console. Log (ARGs. Concat (['A', 'B', 'C']); // works

}

Test (1, 2, 3 );

  1. Log 3
  1. Log [1, 2, 3, "A", "B", "C"]

Function A () {console. Log (arguments. callee )}

A ()

Functiona () {console. Log (arguments. callee )}

 

Function A () {console. Log (arguments. Caller )}

A ()

Undefined

 

Function A () {console. Log (A. Caller )}

A ()

Functioneval () {[native code]}

 

Function A () {console. Log (A. callee )}

A ()

Undefined

 

 

Myfunc. Apply (OBJ, arguments ).

 

// Concat arguments onto

Array. Prototype. Concat. Apply ([1, 2, 3], arguments ).

 

// Turn arguments into a true Array

VaR ARGs = array. Prototype. Slice. Call (arguments );

 

// Cut out first argument

ARGs = array. Prototype. Slice. Call (arguments, 1 );


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.