typeof
The operator returns a string representing the type of the unevaluated operand
- Typeof For basic types, all but NULL can display the correct type.
- Typeof displays object for all objects except functions
- All constructors except Function are of type ‘object’
In the original implementation of JavaScript, values in JavaScript were represented by a tag representing a type and actual data values. The type label of the object is 0. Since null represents a null pointer (0x00 on most platforms), null’s type label is 0 and typeof NULL therefore returns “object”.
instanceof
Is to determine whether the variable is an instance of an object and returns true or false
instanceof (A,B) = { var L = A.__proto__; var R = B.prototype; If (L === R) {//A's internal attribute __proto__ points to B's prototype object return true; } return false; }Copy the code
inheritance
function Parent(){}; function Child(){}; function Other(){}; Child.prototype = new Parent(); let child = new Child(); child instanceof Child; // true child instanceof Parent; // true child instanceof Object; // true child instanceof Other; // false // Cause parent.prototype.__proto__ === object.prototypeCopy the code
Object.prototype.toString.call
Each object has a toString() method, which is called automatically when the object is represented as a text value, or when an object is referenced as an expected string. By default, the toString() method is inherited by each Object. If this method is not overridden in a custom object, toString() returns “[Object type]”, where type is the type of the object
Other types will rewrite the toString method, so return to other values, then use the Object. The prototype. ToString. Call the other types will be returned to the corresponding [Object type]