First of all, everyone should know that in JavaScript only the object has the ToString method, the value of the calling method is converted to a string return, as follows:
var arr = [1, 2, 3]; // the
But the ToString method of the object differs from other advantages in that it returns a string similar to ' [Object class] '
First, in JS, to determine what kind of an object belongs to which built-in type, the most reliable way is through the Object.prototype.toString () method
var arr = [1, 2, 3]; // [Object Array]
Second, Object.prototype.toString () Principle:
Explanation of the Object.prototype.toString () method in ES3
15.2.4.2 Object.prototype.toString ()
When the toString method is called, the following procedure is performed:
1. Gets the value of the [[Class]] property of this object.
2. Calculated three string "[object", the first step of the operation result (1), and "]" after the connection of the new string.
3. Returns the result of the operation of the second step (2).
[[Class]] is an intrinsic property that is owned by all objects (native and host objects). [[Class]] is an intrinsic property that indicates the type of the object whose value type is a string.
The value of the [[class]] property of all built-in objects is defined by this specification. The value of the [[class]] property of all host objects can be any value, even the value of the [[class]] property used by the built-in object. The value of the [[Class]] property can be used to determine which built-in type a native object belongs to. It should be noted that in addition to the Object.prototype.toString method, this specification does not provide any other means for the program to access the value of the property ( View 15.2.4.2).
That is, the string returned by the Object.prototype.toString method is removed from the previous fixed "[object" and the fixed "]", which is the value of the internal property [[class]]. It also achieves the purpose of judging the object type. Tool Methods in jquery $.type (), that's what this is about.
In ES3, the canonical document does not summarize the total number of [[Class]] intrinsic properties, but we can count on our own that there are 10 types of values for the native object's [[Class]] intrinsic properties. Respectively:,,, "Array"
"Boolean"
"Date"
"Error"
, "Function"
, "Math"
, "Number"
,"Object"
, "RegExp"
,"String".
Explanation of the Object.prototype.toString () method in ES5
In ES5.1, there are changes to the definition of Object.prototype.toString methods and [[Class]] internal properties In addition to the more detailed specification writes, the Object.prototype.toString method is as follows:
15.2.4.2 Object.prototype.toString ()
When the toString method is called, the following procedure is performed:
- If The value of this is undefined, it is returned
"[object Undefined]"
.
- If The value of this is null, it is returned
"[object Null]"
.
- Let O become the result of calling Toobject (this) .
- Let class be the value of the internal property of O [[Class]].
- Returns a new string of three strings "[object", class, and "]" after the connection
.
As you can see, more than ES3.1th, 2 steps belong to the new rule, which is more special, because " Undefined"
and" Null"
does not belong to the [[Class]] property value, it should be noted that this is irrelevant to strict mode (most functions in strict mode, The value of this will remain undefined or null, and will automatically become the global object in non-strict mode. The 3rd step is not a new rule, because in ES3 's engine, the three primitive value types are converted to the corresponding wrapper objects, but not written in the specification. ES5, the [[Class]] property is explained in more detail:
The value of the [[class]] property of all built-in objects is defined by this specification. The value of the [[class]] property of all host objects can be in addition to "Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "number", "Object", "RegExp", "string" any string other than. [[Class]] internal properties are used internally by the engine to determine what type of value an object belongs to. It should be noted that in addition to the Object.prototype.toString method, this specification does not provide any other means for the program to access the value of the property (View 15.2.4.2).
Compared with ES3, the first difference is that [[class]] internal properties of the value of two more, into 12 kinds, one is the [[Class] of the Arguments object is "Arguments", not the previous "object", there is more than the global object JSON , its [[Class]] value is "JSON". The second difference is that the value of the [[class]] intrinsic property of the host object cannot conflict with these 12 values, but in browsers that support ES3, it seems that there are no other host objects that intentionally use those 10 values.
Reference article: http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html
Why can the ToString method be used to distinguish between arrays and objects?