//1 Determining if the array type varA=[0]; Console.log (IsArray (a)); functionIsArray (obj) {return(typeofobj== ' object ') && obj.constructor==Array; } console.log ("2-----------------------------------") //2 Determining whether a string typeConsole.log (isstring (' Test '))); Console.log (Isstring (10)); functionisstring (str) {return(typeofstr== ' string ') &&str.constructor==String; } console.log ("3-----------------------------------") //3 Determining whether a numeric typeConsole.log (isnumber (' Test '))); Console.log (Isnumber (10)); functionisnumber (obj) {return(typeofobj== ' number ') &&obj.constructor==Number ; } console.log ("4-----------------------------------") //4 Determining whether a date typeConsole.log (IsDate (NewDate ())); Console.log (IsDate (10)); functionisDate (obj) {return(typeofobj== ' object ') &&obj.constructor==Date; } console.log ("5-----------------------------------") //5 Determining whether a functionConsole.log (Isfunction (functionTest () {})); Console.log (Isfunction (10)); functionisfunction (obj) {return(typeofobj== ' function ') &&obj.constructor==Function; } console.log ("6-----------------------------------") //6 Deciding whether to objectConsole.log (IsObject (NewObject ())); Console.log (IsObject (10)); functionIsObject (obj) {return(typeofobj== ' object ') &&obj.constructor==Object; } /** NULL Array Date object returned with type Object*/
//JS Data type: Number string Boolean undefined object and function type functionsConsole.log (typeof NULL)//ObjectConsole.log (typeofFunction)//function function is a wrapper typeConsole.log (typeofUndefined//undefined varmessage; Console.log (message)//undefined defines a value that is not assignedConsole.log (typeofMessage//Undefined typeConsole.log (NULL= = undefined)//true /** TRUE==1;FASE==0; * */Console.log (true= = 1)//trueConsole.log (false= = 0)//trueConsole.log (false==2)//false varStr= ""; Console.log (str==0);//true; the empty string will convert the value to 0Console.log (typeofStr//string
var str2=new String (' A ');
Console.log (STR2)//String {0="A"}
Prototype extension methods
Object.prototype.toString.call
var gettype=object.prototype.tostring gettype.call (' aaaa ') //[object String] gettype.call (2222) [Object number] Gettype.call (true) //[object Boolean] gettype.call (undefined) //[object Undefined] gettype.call (null) //[object NULL] gettype.call ({}) //[object object] Gettype.call ([]) //[object Array] gettype.call (function () {}) //[object function]
/*In fact JS There are many types of judgment [object Htmldivelement] Div object, [object Htmlbodyelement] Body object, [object Documen T] (IE) or [object HTMLDocument] (firefox,google) ... Various DOM nodes are judged, and these things are used when we write plugins. */ varGettype=Object.prototype.toStringvarutility={isobj:function(o) {returnGettype.call (o) = = "[Object Object]"; }, IsArray:function(o) {returnGettype.call (o) = = "[Object Array]"; }, IsNULL:function(o) {returnGettype.call (o) = = "[Object Null]"; }, Isdocument:function(){ returnGettype.call (o) = = "[Object Document]" | |[Object HTMLDocument]; } //...}
JavaScript Learning Notes--type judgment