series
Front end Stereotype – prototype chain
Front end octuwen – Inheritance
Front – Write new, bind, call and apply by hand
Typeof (cannot correctly determine the type)
- For primitive types, all but NULL can display the correct type
- For objects, all but functions display object
- For NULL, object is displayed
Instanceof (invalid original type)
Usage: object (the object to be detected) instanceof constructor
Constructor: used to check whether constructor. Prototype exists on the prototype chain of the parameter Object
Important: Another, more important point is that instanceof can be used in inheritance relationships to determine whether an instance is of its parent type. Its essence is layer upon layer retrieval on the prototype chain
Normal use cases:
const Person = function() {} const p1 = new Person() p1 instanceof Person // true var str = 'hello world' str instanceof String // false var str1 = new String('hello world') str1 instanceof String // trueCopy the code
For primitive types, you don’t want instanceof to determine the type directly, but there are ways to let Instanceof determine the primitive type
class PrimitiveString {
static [Symbol.hasInstance](x) {
return typeof x === 'string'
}
}
console.log('hello world' instanceof PrimitiveString) // true
Copy the code
Key code use cases:
Function Aoo() {// constructor Foo Foo() {} // constructor Foo Foo. Prototype = new Aoo(); Var Foo = new Foo(); / / constructor Foo instance / / Foo __proto__ = = = Foo prototype / / Foo __proto__. __proto__. = = = Aoo the prototype console. The log (Foo __proto__ === Foo.prototype); / / __proto__ pointing to the prototype of the constructor console. The log (foo. __proto__. __proto__. = = = Aoo the prototype); Console. log(foo instanceof foo)//true Foo's prototype is mounted in instance console.log(foo instanceof Aoo)//true // Foo. Prototype then goes to ___proto__ and finds aoo. prototype, thus trueCopy the code
Object.prototype.toString.call()
The toString method converts an object to a string of the form “[object XXX]”.
For most objects, the toString() method is overridden, in which case it needs to be called with a call() or reflect.apply () method.
For the Object. The prototype. ToString. Call (arg), if the parameter is null or undefined, return the result directly.
If the parameter is not null or undefined, the parameter is converted into an object and then judged.
Note: Primitive datatype. When calling a method, the primitive datatype is converted to a wrapper object before the method call
var a = '1'
a.length => new String('1').length
Copy the code
Core principles
After being converted to an object, the object’s [symbol.toStringTag] attribute value (possibly traversing the prototype chain) is obtained as a tag. If there is no such attribute, or if the attribute value is not a string, the tag is obtained as follows and the string of “[object “+ tag + “]” is returned. Attribute value is expected to be a string, otherwise ignored.
Type detection list
/ / a Boolean type, the tag for "Boolean" Object. The prototype. ToString. Call (true); / / = > "[object Boolean]" / / type Number, tag for "Number" is the object. The prototype. ToString. Call (1); / / = > "[object Boolean]" / / type String, the tag for "String" is the object. The prototype. ToString. Call (" "); / / = > "[object String]" / / an Array type, the tag for "String" is the object. The prototype. ToString. Call ([]); / / = > "[object Array]" / / type of the Arguments, the tag for "Arguments" is the object. The prototype. ToString. Call ((function () {return the Arguments; }) ()); / / = > "[object the Arguments]" / / Function type, the tag for the "Function" is the object. The prototype. ToString. Call (Function () {}); / / = > "[object Function]" / / the Error types (including subtype), the tag for "Error" is the object. The prototype. ToString. Call (new Error ()); / / = > "[object Error]" / / RegExp type, the tag for "RegExp" object. The prototype. ToString. Call (\ d + /); / / = > "[object RegExp]" / / Date type, the tag for "Date" is the object. The prototype. ToString. Call (new Date ()); / / = > "[object Date]" / / other types, the tag for "object" is the object. The prototype. ToString. Call (new class {}); // => "[object Object]"Copy the code
An example of overriding symbol.tostringTag
var o1 = { [Symbol.toStringTag]: "A" };
var o2 = { [Symbol.toStringTag]: null };
Object.prototype.toString.call(o1); // => "[object A]"
Object.prototype.toString.call(o2); // => "[object Object]"
Copy the code