JavaScript Learning Notes--type judgment

Source: Internet
Author: User

//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

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.