详解JavaScript中的4种类型识别方法
作者:bea
具体内容如下: 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
具体内容如下:
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,会报错
【实例】
console.log("jerry" instanceof String);//false
console.log(12 instanceof Number);//false
console.log(true instanceof Boolean);//false
//console.log(undefined instanceof Undefined);//报错
//console.log(null instanceof Null);//报错
console.log({name: "jerry"} instanceof Object);//true
console.log(function(){} instanceof Function);//true
console.log([] instanceof Array);//true
console.log(new Date instanceof Date);//true
console.log(/d/ instanceof RegExp);//true
function Person(){};
console.log(new Person instanceof Person);//true
console.log(new Person instanceof Object);//true
以上内容就是详解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,会报错
【实例】
console.log("jerry" instanceof String);//false
console.log(12 instanceof Number);//false
console.log(true instanceof Boolean);//false
//console.log(undefined instanceof Undefined);//报错
//console.log(null instanceof Null);//报错
console.log({name: "jerry"} instanceof Object);//true
console.log(function(){} instanceof Function);//true
console.log([] instanceof Array);//true
console.log(new Date instanceof Date);//true
console.log(/d/ instanceof RegExp);//true
function Person(){};
console.log(new Person instanceof Person);//true
console.log(new Person instanceof Object);//true
以上内容就是详解JavaScript中的4种类型识别方法,希望大家喜欢。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 基于jQuery滑动杆实现购买日期选择效果
- JavaScript 封装一个tab效果源码分享
- jQuery简单实现两级下拉菜单效果代码
- 黑帽seo劫持程序,js劫持搜索引擎代码
- JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
- JS实现仿FLASH效果的竖排导航代码
- JS+CSS实现自适应选项卡宽度的圆角滑动门效果
- JS实现完全语义化的网页选项卡效果代码
- js如何判断访问是来自搜索引擎(蜘蛛人)还是直接访问
- 根据user-agent判断蜘蛛代码黑帽跳转代码(js版与php版本)
- jQuery实现默认是闭合的FAQ展开效果菜单
- js带缩略图的图片轮播效果代码分享
- jQuery实现的Tab滑动选项卡及图片切换(多种效果)小结
- JS+DIV+CSS实现的经典标签切换效果代码
- jQuery实现Meizu魅族官方网站的导航菜单效果
- jQuery实现鼠标悬停背景翻转的黑色导航菜单代码
- JS+CSS实现自动切换的网页滑动门菜单效果代码
- JavaScript人脸识别技术及脸部识别JavaScript类库Tracking.js
- JS实现可关闭的对联广告效果代码