javascript--Standard Functions

Source: Internet
Author: User
Tags gmt time zone local time wrapper



(1) Do not usenew Number(),new Boolean()andnew String()create packaging objects;



(2) UseparseInt()orparseFloat()to convert any type tonumber;



(3) AString()method for converting any type tostring, or invoking an object directlytoString();



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



(5)typeofOperators can determine the,,,numberbooleanstringfunctionandundefined;



(6) JudgmentArrayto be usedArray.isArray(arr);



(7) Judgingnullplease usemyVar === null;



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



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



Package object:


Var n = new Number(123); // 123, a new wrapper type is generated
Var b = new Boolean(true); // true, a new wrapper type is generated
Var s = new String(‘str‘); // ‘str‘, generated a new wrapper type 
 
 
typeof new Number(123); // ‘object‘
new Number(123) === 123; // false

typeof new Boolean(true); // ‘object‘
new Boolean(true) === true; // false

typeof new String(‘str‘); // ‘object‘
new String(‘str‘) === ‘str‘; // false


Note:nullandundefinedThere is notoString()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 notDatean object, but a timestamp.



Convert the timestamp to aDate:


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) {
     Console.log(Date.now()); // Older versions of IE do not have a 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.

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.