Home  >  Article  >  Web Front-end  >  Why use Object.prototype.toString.call(obj) to detect object type?

Why use Object.prototype.toString.call(obj) to detect object type?

青灯夜游
青灯夜游forward
2018-10-10 17:27:452432browse

I recently did some js interviews 25 Essential JavaScript Interview Questions*, the first of which is: use typeof bar === "object" to detect whether "bar" is an object. shortcoming? How to avoid it?

This is a very common question. Can typeof be used to accurately determine an object variable? The answer is no. The result of null is also object. The result of Array is also an object. Sometimes what we need is a "pure" object object. How to avoid it? A better way is:

console.log(Object.prototype.toString.call(obj) === "[object Object]");

Use the above method to distinguish various types:

(Custom object types cannot be distinguished, custom types can be distinguished by instanceof)

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]

Why can we distinguish it like this? So I took a look at the usage of the toString method: the toString method returns a string that reflects this object.

Then why not use obj.toString() directly?

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());//error
console.log(undefined.toString());//error

It is also the detection object obj that calls the toString method (for the usage of the toString() method, please refer to the detailed explanation of toString), the result of obj.toString() is the same as Object. The result of prototype.toString.call(obj) is different , why is this?

This is because toString is the prototype method of Object, and Array, function and other types are rewritten as instances of ObjecttoString method. When different object types call the toString method, based on the knowledge of the prototype chain, the corresponding rewritten toString method is called (the function type returns a string whose content is the function body, The Array type returns a string composed of elements...), instead of calling the prototype toString method on Object (returning the specific type of the object), so obj.toString()# is used ##You cannot get its object type, you can only convert obj to a string type; therefore, when you want to get the specific type of the object, you should call the prototype toString method on Object.

We can verify it by deleting the toString method of the array and see what the result will be:

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]"

After deleting the toString method of the Array, the same When the arr.toString() method is used again, there is no instance method that shields the Object prototype method. Therefore, along the prototype chain, arr finally calls the Object's toString method, returning and

Object.prototype.toString.call(arr)Same result.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit

JavaScript Video Tutorial!

Related recommendations:

php public welfare training video tutorial

JavaScript graphic tutorial

JavaScriptOnline Manual

The above is the detailed content of Why use Object.prototype.toString.call(obj) to detect object type?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete