{}.toString.call(obj)判断数据类型

2021-09-19  本文已影响0人  曲昶光

先看一下typeof

var a;
console.log("a:"+typeof a)//a:undefined
 var b=null;
console.log("b:"+typeof  b)//b:object
 var c=undefined;
console.log("c:"+typeof c)//c:undefined
 var d=new Object();
console.log("d:"+typeof d)//d:object
 var e=function(){};
console.log("e:"+typeof e)//e:function
 var f={};
console.log("f:"+typeof f)//f:object
 var g="";
console.log("g:"+typeof g)//g:string
 var h=[];
console.log("h:"+typeof h)//h:object
 var i=true;
console.log("i:"+typeof i)//i:boolean
 var j=123
 console.log("j:"+typeof j)//j:number
 var k=NaN;
console.log("k:"+typeof  k)//k:number
 var l=/^[-+]?\d+$/;
console.log("l:"+typeof  l)//l:object

按照上面的打印结果,总结出以下几点:
1 除了函数 function,typeof ‘引用类型’ 都是object 例如type [];typeof /^\d$/ ;typeof null

2 typeof null 为object ,typeof NaN 是number,typeof undefined是undefined,typeof function(){}是function

3 转换数字的常见用法“10”-0,如果没有转换成功则返回NaN,由于NaN的一个特性NaN!=NaN

故判断转换成功与否的常见做法是:('10sss'-0)==('10sss'-0)//结果为假 *******

再看看constructor和instanceof

instanceof 用于判断一个对象是否是某个类的实例

constructor 用于判断对象的原型,该属性返回对创建此对象的数组函数的引用。

   console.log("-------------------Number----------------")
    var A=123;
   console.log(A instanceof Number)//false
    console.log(A.constructor===Number)//true
    console.log(A.constructor)
    console.log("-----------------String------------------")
    var B="javascript";
   console.log(B instanceof String)//false
    console.log(B.constructor===String)//true
    console.log(B.constructor)
   console.log("-----------------Boolean------------------")
    var C=true;
   console.log(C instanceof Boolean)//false
    console.log(C.constructor===Boolean)//true
    console.log(C.constructor)
    console.log("------------------null------------------------")
   var D=null
    console.log(D instanceof Object)//false
//    console.log(D.constructor===Object)//报错 null没有构造函数
   //console.log(D.constructor)//报错
console.log("----------------------undefined---------------------")
    var E=undefined;
//   console.log(E instanceof undefined)//报错 没有undefined数据类
    console.log("---------------------function---------------------")
    var F=function(){}
    console.log(F instanceof Function)//true
    console.log(F.constructor=== Function)//true
    console.log(F.constructor)
console.log("---------------------new function----------------------")
    function SB(){}
    var G=new SB();
    console.log(G instanceof SB)//true
    console.log(G.constructor==SB)//true
    console.log(G.constructor)
console.log("----------------------new Object------------------------")
    var H=new Object;
    console.log(H instanceof Object)//true
    console.log(H.constructor==Object)//true
    console.log(H.constructor)
    console.log("---------------------Array---------------------------")
    var I=[];
    var H=new Array();
    console.log(I instanceof Array);//true
    console.log(H instanceof Array);//true
    console.log(I.constructor==Array)//true
    console.log(H.constructor==Array)//true
    console.log(H.constructor)//true
    console.log(I.constructor)//true
    console.log("--------------------------------JSON---------------")
    var J={
        "sb":"javascript",
        "node":"very SB"
    }
    console.log(J instanceof Object)//true
    console.log(J.constructor===Object)//true
    console.log(J.constructort)//true

再看看{}.toString.call(obj)

console.log({}.toString.call(1))//[object Number]
console.log({}.toString.call("11"))//[object String]
console.log({}.toString.call(/123/))//[object RegExp]
console.log({}.toString.call({}))//[object Object]
console.log({}.toString.call(function(){}))//[object Function]
console.log({}.toString.call([]))//[object Array]
console.log({}.toString.call(true))//[object Boolean]
console.log({}.toString.call(new Date()))//[object Date]
console.log({}.toString.call(new Error()))//[object Error]
console.log({}.toString.call(null))//[object Null]
console.log({}.toString.call(undefined))//[object Undefined]
console.log(String(undefined))//undefined
console.log(String(null))//null
上一篇下一篇

猜你喜欢

热点阅读