- Defining a constructor
Each function has an explicit prototype that is automatically added when the function is defined. The default value is an empty Object Object
function Fn() {
// internal autoexecute statement: this.prototype = {}
}
console.log(Fn.prototype)
Copy the code
- Creating instance Objects
Each instance object has a __proto__, the implicit prototype, which is added automatically when the object is created and defaults to the constructor’s Prototype property value
var fn = new Fn() // Internal automatic execution statement: this.__proto__ = fn.prototype
console. log(fn.__proto__)
Copy the code
The implicit stereotype of an object is the explicit stereotype of its corresponding constructor
console.log(Fn.prototype === fn.__proto__) // true
Copy the code
- Add methods to the prototype
Fn.prototype.test = function () {
console.log('test')}Copy the code
- Invoke the prototype method from the instance object
fn.test() / / print test
Copy the code
- Explicit and implicit archetype relationships