javascript--Standard Functions

Source: Internet
Author: User

Tags: use get wrapper class database Func int assume number browser

(1) Do not use new Number() , new Boolean() and new String() create packaging objects;

(2) Use parseInt() or parseFloat() to convert any type to number ;

(3) A String() method for converting any type to string , or invoking an object directly toString() ;

(4) There is usually no need to convert any type to boolean re-judgment, because it can be written directly if (myVar) {...} ;

(5)typeofOperators can determine the,,, number boolean string function and undefined ;

(6) Judgment Array to be used Array.isArray(arr) ;

(7) Judging null please use myVar === null ;

(8) Determining whether a global variable exists typeof window.myVar === ‘undefined‘ ;

(9) The function internally determines whether a variable exists typeof myVar === ‘undefined‘ .

Package object:

var New // 123, a new packaging type is generated var New Boolean (true//  True, generates a new wrapper type varnew//  ' str ' , a new wrapper type is generated
typeof NewNumber (123);//' object 'NewNumber (123) = = = 123;//falsetypeof NewBoolean (true);//' object 'NewBoolean (true) ===true;//falsetypeof NewString (' str ');//' object 'NewString (' str ') = = = ' str ';//false

Note: null and undefined There is no toString() way

// SyntaxError

Workaround:

// ' 123 ', attention is two points!  //  ' 123 '

Date

JavaScript Date Object month value starting from 0, remember 0=1 month, 1=2 month, 2=3 month, ..., 11=12 month.

var New Date (5, +, 123//  Fri June 20:15:30 gmt+0800 (CST)

When using Date.parse (), the passed-in string uses the actual month 01~12, and getmonth () gets the month value 0~11 after the conversion to the date object.

var d = date.parse (' 2015-06-24t19:49:22.875+08:00 '//  1435146562875

But it returns not Date an object, but a timestamp.

Convert the timestamp to a Date :

var New Date (1435146562875//  Wed June 19:49:22 gmt+0800 (CST)//  5

The timestamp is an auto-increment integer that represents the moment that the GMT time zone starts at zero January 1, 1970, and the current number of milliseconds. Assuming that the time of the browser's computer is accurate, the time stamp numbers in the world, regardless of the time zone, are the same at the moment, so the timestamp can represent exactly one moment and is independent of the time zone.

So, we just need to pass the timestamp, or read the timestamp out of the database and let JavaScript automatically convert to local time.

Gets the current timestamp:

' Use strict '; if (date.now) {    //  old version ie does not have now () method Else  {    console.log (New  Date (). GetTime ());}

javascript--Standard Functions

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.

Tags Index: