类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()

1.typeof只能判断基本类型数据,

例子:

typeof 1 // "number"
typeof ‘1‘ // "string"
typeof true // "boolean"
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"
typeof undefined // "undefined"
typeof null // "object",注意!!!

2.instanceof

let obj = new Object()

obj instanceof Object // true

注意:instanceof左侧必须是对象,如果不是直接返回false,

例如:

let num = 1
num instanceof Number // false

num = new Number(1)
num instanceof Number // true

3.最好的类型判断方法:Object.prototype.toString.call(需要判断的值)

console.log(Object.prototype.toString.call(Object) === "[object Object]");
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]

为什么这样就能区分呢?

toString方法返回反映这个对象的字符串。

那为什么不直接用obj.toString()呢?

console.log("jerry".toString());//jerry
console.log((1).toString());//1
console.log([1,2].toString());//1,2
console.log(new Date().toString());//Wed Dec 21 2016 20:35:48 GMT+0800 (中国标准时间)
console.log(function(){}.toString());//function (){}
console.log(null.toString());//报错
console.log(undefined.toString());//报错

同样是检测对象obj调用toString方法,obj.toString()的结果和Object.prototype.toString.call(obj)的结果不一样,这是为什么?

这是因为toString为Object的原型方法,而Array ,function等类型作为Object的实例,都重写了toString方法。

不同的对象类型调用toString方法时,根据原型链的知识,调用的是对应的重写之后的toString方法(function类型返回内容为函数体的字符串,Array类型返回元素组成的字符串.....),而不会去调用Object上原型toString方法(返回对象的具体类型),所以采用obj.toString()不能得到其对象类型,只能将obj转换为字符串类型;因此,在想要得到对象的具体类型时,应该调用Object上原型toString方法。

验证方法:将数组的toString方法删除

var arr=[1,2,3];
console.log(Array.prototype.hasOwnProperty("toString"));//true
console.log(arr.toString());//1,2,3
delete Array.prototype.toString;//delete操作符可以删除实例属性
console.log(Array.prototype.hasOwnProperty("toString"));//false
console.log(arr.toString());//"[object Array]"

删除了Array的toString方法后,同样再采用arr.toString()方法调用时,不再有屏蔽Object原型方法的实例方法,因此沿着原型链,arr最后调用了Object的toString方法,返回了和Object.prototype.toString.call(arr)相同的结果。

封装:

1.原理:截取[object type]------slice(8,-1)从第下标8开始截取,到下标倒数第一个

2.封装成一个函数:

//返回传递给他的任意对象的类 function isClass(o){ return Object.prototype.toString.call(o).slice(8,-1); }

3.使用:

参考:

https://www.cnblogs.com/youhong/p/6209054.html

https://www.cnblogs.com/Yance/p/7655320.html

原文地址:https://www.cnblogs.com/echo-hui/p/9551472.html

时间: 2024-08-09 17:48:53

类型判断----小白讲解typeof,instanceof,Object.prototype.toString.call()的相关文章

判断对象类型 typeof instanceof Object.prototype.tostring()

常见的有三种方法  1, typeof  2, instance of   3, object.prototype.toString.apply(); 1,typeof  typeof 运算符 typeof 是一元运算符,返回结果是一个说明运算数类型的字符串.如:"number","string","boolean","object","function","undefined"(可用于

typeof 、Object.prototype.toString和 instanceof

数据类型 js 基本类型包括:Undefined  symbol null string boolean number js 引用类型包括:object array Date RegExp typeof 我们一般用typeof来判断数据的类型的 接下来我们试试 console.log(typeof undefined) //undefined console.log(typeof null) //object console.log(typeof Undefined) //undefined c

typeof()与Object.prototype.toString.call()

用typeof方法只能初步判断number string undefined boolean object function symbol这几种初步类型 使用Object.prototype.toString.call(var) 能判断具体的类型数组,函数 1 var arr = [1,2,3]; 2 typeof(arr); 3 object.prototype.toString.call(arr) 原文地址:https://www.cnblogs.com/angle-yan/p/116156

JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转化为string类型: console.log(true.toString());//"true" console.log(false.toString());//"false&quo

Object.prototype.toString.call()方法浅谈

参考链接:http://www.cnblogs.com/wyaocn/p/5796142.html 使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//"[object Null]" Object.prototype.toString.call(undefined);/

Object.prototype.toString.call()方法

使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//"[object Null]" Object.prototype.toString.call(undefined);//"[object Undefined]" Object.prototype.t

Object.prototype.toString.call()

使用Object.prototype上的原生toString()方法判断数据类型,使用方法如下: Object.prototype.toString.call(value) 1.判断基本类型: Object.prototype.toString.call(null);//”[object Null]” Object.prototype.toString.call(undefined);//”[object Undefined]” Object.prototype.toString.call(“a

判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解

转载自[脚本之家],原文链接:http://www.jb51.net/article/42864.htm 提醒大家,Object.prototype.toString().call(param)返回的[object class]中class首字母是大写,像JSON这种甚至都是大写,所以,大家判断的时候可以都转换成小写,以防出错 1.typeof(param) 返回param的类型(string) 这种方法是JS中的定义的全局方法,也是编译者们最常用的方法,优点就是使用简单.好记,缺点是不能很好的

判断js中的类型:typeof / instanceof / constructor / prototype

如何判断js中的类型呢,先举几个例子: var a = "jason"; var b = 123; var c = true; var d = [1,2,3]; var e = new Date(); var f = function(){ alert('jason'); }; 一.最常见的判断方法:typeof typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果,另外typeof可以判断function的类型:在判断除Object类型的对象时