"Notes" JavaScript Coding specification-type Assignment & coercion

Source: Internet
Author: User

Executes a coercion type conversion statement.

String

Bad <p>//= This.reviewscore = 9;</p>var a= this.reviewscore + ';//Goodvar Totalscore = ' + This.rev iewscore;//Badvar totalscore = "+ This.reviewscore + ' total score ';//Goodvar Totalscore = this.reviewscore + ' Total Score ';


use parseint to convert numbers and take a binary as a parameter

var a= ' 4 ';//Badvar val = new number (a);//Badvar val = +a;//Badvar val = a>> 0;//Badvar val = parseint (a);//Goo DVar val = number (a);//Goodvar val = parseint (A, 10);


for whatever reason, perhaps you have done something "rude", perhaps parseint has become your bottleneck, perhaps considering performance and the need to use bit arithmetic, use annotations to explain why you did it.
good/*** parseint was the reason my code is slow.* bitshifting the String to coerce it to a * number made it a lot fast Er.*/var val = inputvalue >> 0;

NOTE: When bitwise operations are used, numbers is treated as a 64-bit value, but the bitwise operation always returns a 32-bit integer (source). A bitwise operation with an integer value greater than 32 bits will result in unpredictable behavior. Discussion. The largest signed 32-bit integer is 2,147,483,647

2147483647 >> 0//=> 21474836472147483648 >> 0//=> -21474836482147483649 >> 0//=> 2147483647
Booleans:

var = 0;//Badvar hasage = new Boolean (age);//Goodvar Hasage = Boolean (age);//Goodvar Hasage =!! Age


1:14 and God Said,let there be lights in the firmament of the heaven to divide the day from the Night;and let them is for Signs,and for Seasons,and for days,and years:



"Notes" JavaScript Coding specification-type Assignment & coercion

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.