Analysis Object.prototype.toString.call (obj) detects the object type Cause

Analysis Object.prototype.toString.call (obj) detects the object type Cause

 Updated: October 11, 2018 08:46:33 Contributor: laozhang     I want to comment
 
In this article we give you in analyzing the reasons for using Object.prototype.toString.call (obj) to detect the type of object, need friends can learn under.

This is a very common problem, whether with typeof can accurately determine an object variable, the answer is no, the result is also null object, Array results also object, sometimes we need is a "pure" object object. How to avoid it? It is a better way:

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

Well distinguish types using the above method:

(Custom objects can not distinguish between types, custom types may be employed to distinguish instanceof)

?
1
2
3
4
5
6
7
8
9
10
11
12
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 you can distinguish it? So I went to see what the toString method of use: toString method returns a string that reflects the object.

Why not just use obj.toString () do?

?
1
2
3
4
5
6
7
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

The same is to detect the object obj call toString method (For the use of toString () method can refer to the Detailed toString), the result obj.toString () results and Object.prototype.toString.call (obj) is not the same, which is why ?

This is because the Object toString prototype method, the Array, function, etc. As an example of Object type are rewritten toString method. When calling toString method different object types, according to the knowledge of the prototype chain, toString method is invoked (function type string function returns the contents of a corresponding body after rewriting, Array type elements returned string .... .), and not to call the prototype method toString (specific type of the returned object) on the object, so using obj.toString () object type can not be obtained, it can be converted to a string type obj; therefore, to obtain the desired the specific type of the object, the prototype object toString method should be called on.

We can verify what will be removed toString array of methods to see what would result:

?
1
2
3
4
5
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 method delete, and then using the same arr.toString () method call, there is no example of a method of shielding Object prototyping, thus along the prototype chain, ARR last call toString method of Object, Object and returned .prototype.toString.call (arr) the same result.

Guess you like

Origin www.cnblogs.com/chargeworld/p/12241707.html