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)