详解JavaScript中的4种类型识别方法

JavaScript中检测对象类型的运算符有:typeof、instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串。下面由小编给大家分享JavaScript中的4种类型识别方法,需要的朋友可以参考下本文

具体内容如下:

1.typeof

  【输出】首字母小写的字符串形式

  【功能】

    [a]可以识别标准类型(将Null识别为object)
    [b]不能识别具体的对象类型(Function除外)

  【实例】

console.log(typeof "jerry");//"string" console.log(typeof 12);//"number" console.log(typeof true);//"boolean" console.log(typeof undefined);//"undefined" console.log(typeof null);//"object" console.log(typeof {name: "jerry"});//"object" console.log(typeof function(){});//"function" console.log(typeof []);//"object" console.log(typeof new Date);//"object" console.log(typeof /\d/);//"object" function Person(){}; console.log(typeof new Person);//"object"

2.Object.prototype.toString

  【输出】[object 数据类型]的字符串形式

  【功能】

    [a]可以识别标准类型及内置对象类型
    [b]不能识别自定义类型

  【构造方法】

function type(obj){   return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); }

  【实例1】

console.log(Object.prototype.toString.call("jerry"));//[object String] console.log(Object.prototype.toString.call(12));//[object Number] console.log(Object.prototype.toString.call(true));//[object Boolean] console.log(Object.prototype.toString.call(undefined));//[object Undefined] console.log(Object.prototype.toString.call(null));//[object Null] console.log(Object.prototype.toString.call({name: "jerry"}));//[object Object] console.log(Object.prototype.toString.call(function(){}));//[object Function] console.log(Object.prototype.toString.call([]));//[object Array] console.log(Object.prototype.toString.call(new Date));//[object Date] console.log(Object.prototype.toString.call(/\d/));//[object RegExp] function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]

  【实例2】

function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); } console.log(type("jerry"));//"string" console.log(type(12));//"number" console.log(type(true));//"boolean" console.log(type(undefined));//"undefined" console.log(type(null));//"null" console.log(type({name: "jerry"}));//"object" console.log(type(function(){}));//"function" console.log(type([]));//"array" console.log(type(new Date));//"date" console.log(type(/\d/));//"regexp" function Person(){}; console.log(type(new Person));//"object"

3.constructor

  【输出】function 数据类型(){[native code]}或者function 自定义类型(){}

  【功能】

    [a]可以识别标准类型、内置对象类型及自定义类型
    [b]不能识别undefined、null,会报错

  【构造方法】

function type(obj){ var temp = obj.constructor.toString(); return temp.replace(/^function (\w+)\(\).+$/,'$1'); }

  【实例1】

console.log(("jerry").constructor);//function String(){[native code]} console.log((12).constructor);//function Number(){[native code]} console.log((true).constructor);//function Boolean(){[native code]} //console.log((undefined).constructor);//报错 //console.log((null).constructor);//报错 console.log(({name: "jerry"}).constructor);//function Object(){[native code]} console.log((function(){}).constructor);//function Function(){[native code]} console.log(([]).constructor);//function Array(){[native code]} console.log((new Date).constructor);//function Date(){[native code]} console.log((/\d/).constructor);//function RegExp(){[native code]} function Person(){}; console.log((new Person).constructor);//function Person(){}

  【实例2】

function type(obj){ var temp = obj.constructor.toString().toLowerCase(); return temp.replace(/^function (\w+)\(\).+$/,'$1'); } console.log(type("jerry"));//"string" console.log(type(12));//"number" console.log(type(true));//"boolean" //console.log(type(undefined));//错误 //console.log(type(null));//错误 console.log(type({name: "jerry"}));//"object" console.log(type(function(){}));//"function" console.log(type([]));//"array" console.log(type(new Date));//"date" console.log(type(/\d/));//"regexp" function Person(){}; console.log(type(new Person));//"person"

4.instanceof

  【输出】true或false

  【功能】

    [a]可以识别内置对象类型、自定义类型及其父类型
    [b]不能识别标准类型,会返回false
    [c]不能识别undefined、null,会报错

  【实例】

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgggzf.html