The prototype

  1. The value of each function data type has a built-in attribute:PrototypeThe property value of this property is an object (Used to store properties and methods common to the instanceFunction prototype is not an object, but an anonymous empty Function (which is also an object).
    • Common function
    • Classes (custom and built-in classes)

The prototype object has a built-in constructor attribute that stores the current function itself

Fn.prototype.constructor === Fn
Copy the code
  1. The value of each object data type also has a built-in attribute: __proto__, which points to the prototype of the class to which it belongs

    • Ordinary objects, objects, arrays, re, Math, Date, class arrays…
    • Instances are objects
    • The function’s prototype is also an object
    • Functions are also values of object data types

Prototype chain lookup mechanism

  1. First look for their own private attributes, then call to use, not continue to find
  2. Based on the__proto__Find the method on the stereotype of the class it belongs to, if not, continue to base on__proto__Up to find… Find Object. Prototype

The problem with this in the prototype chain approach

  1. Method, if there’s a dot in front of it, it’s this
  2. Replace this in the method with the one before the dot
  3. Then the result is determined by the method of prototype chain search
function Fn(){
    //this: f1
    this.x = 100
    this.y = 200
    this.say = function(){
        console.log(this.x)
    }
}
Fn.prototype.say = function(){
    console.log(this.y)
}
Fn.prototype.eat = function(){
    console.log(this.x + this.y)
}
Fn.prototype.write = function(){
    this.z = 1000
}
let f1 = new Fn
f1.say() //this: f1 => f1.x => 100
f1.eat() //this: f1 =>f1.x + f1.y => 300
f1.__proto__.say() //this: f1.__proto__ => f1.__proto__.x => undefined
Fn.prototype.eat() //this: Fn.prototyp => Fn.prototyp.x + Fn.prototyp.y => NaN
f1.write() //this: f1 => f1.z = 1000=> set a private attribute for f1
Fn.prototype.write() //this: fn. prototype => fn.prototype. z => set a public attribute for f1
Copy the code

hasOwnProperty

Checks whether an attribute name is of the current objectPrivate property

In: Checks whether an attribute belongs to an object (true if it is a private or public attribute)

Private properties: Properties owned by the instance in its own heap that do not need to be looked up on the stereotype chain

Public attributes: Attributes that the instance can only find from the stereotype chain

let ary = [10.20.30]
console.log('0' in ary)  //true
console.log('push' in ary)  //true
console.log(ary.hasOwnProperty('0'))  //true
console.log(ary.hasOwnProperty('push'))  // False push is public
console.log(Array.prototype.hasOwnProperty('push')) // Whether true is a public or private attribute depends on whom you look at it
console.log(Array.prototype.hasOwnProperty('hasOwnProperty'))  //false
console.log(Object.prototype.hasOwnProperty('hasOwnProperty'))  //true hasOwnProperty is the private property of Object
Copy the code