JavaScript type conversion (this post from NetEase front-end micro-professional personal study notes)

Source: Internet
Author: User

Small words: If you really want to write a good JS, first understand the JS type conversion. First paste out the three categories of JS object.

1. Browser extension object: Activexobject,xml,debug,script,vbarray ...

2. Host object: Window,document,navigator

3. Native objects:
(1). Constructor: Boolean,string,nunber,object,function,array,date,regexp,error
(2). Object: Math,json, Global object, arguments

First, JS implicit type conversion

JS is a very flexible, weakly typed language, so the type conversion is done automatically. The implicit type conversions that I record are usually divided into several scenarios:

(1). The direct amount of the dot character is implicitly converted to an object. Example:var str = ' abc '; Str.indexof ();

(2). An implicit conversion in an if () expression is example:var str = '; if (Boolean (str)) {...}; The string is implicitly converted.

(3). ==,+,-, will also be implicitly converted Example:var str = ' abc '; var num = 123; alert (str+num);

Two or four methods of type recognition (typeof, Instanceof, Object.prototype.toString.call, constructor)

  

Write a simple distinction between the JS types of incoming parameters

/*
* Input Format:
* ' 2015-08-05 '
* 1438744815232
* {Y:2015,m:8,d:5}
* [2015,8,5]
* Return Format: Date
*/
function ToDate (param) {
if (typeof (param) = = ' String ' | | typeof (param) = = ' number ') {
return new Date (param);
}
if (param instanceof Array) {
var date = new Date ();
Date.setyear (Param[0]);
Date.setmonth (Param[1]-1);
Date.setdate (param[2]);
return date;
}
if (typeof (param) = = = ' object ') {
var date = new Date ();
Date.setyear (PARAM.Y);
Date.setmonth (param.m-1);
Date.setdate (PARAM.D);
return date;
}

return-1;
}

JavaScript type conversion (this post from NetEase front-end micro-professional personal study notes)

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.