Have you seen any of these javascript traps?

Source: Internet
Author: User

First, Member operators

 number.prototype.testfn=function   () { Console.log ( this  <0, this  Span style= "color: #000000;" >.valueof ());}   var  num = -1;num.testfn ();   // true-1  (). TESTFN (); //  false  22.testFn (); // uncaught syntaxerror:invalid or unexpected token   ( -1). TESTFN (); //  true-1  -1..testfn (); //  false 1  -1.2.testfn (); // false 1.2  

The dot operator is prioritized as part of a numeric constant, and then is the object property accessor.

Second, even equal assignment value

var a = {N:1};   var b == a = {N:2};  Console.log (a.x);   // undefined   Console.log (b.x);  // {N:2}

Exchange the order of the next hyphen, a = A.x = {N:2}; You can see that the result is the same, that the order does not affect the result.

Iii. Forced conversions

var areg =/^[a-z]+$/; areg.test (null);  // trueareg.test ();  // true

If the parameter of the test method is not a string, it is coerced into a string by an abstract ToString operation, so the string "null" and "undefined" are actually tested.

Iv. Communication of parameters

"1 2 3". Replace (/\d/g, parseint);  // "1 NaN 3"

The actual calculations are [1, 0], [2, 2], [3, 4]

Why is adding attributes to the underlying type not an error but invalid?

var num = 1= 2; num.prop   //undefined

Num.prop is equivalent to object (NUM). Prop, that is, we add attributes to a newly created object that has no relation to Num.

Have you seen any of these javascript traps?

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.