Create an object

A single create

If you want to create multiple objects, there’s a lot of repetitive code

const a = new Object();
a.name = 'hahah';

// 字面量创建
const a = { name: 'hahha'}; 
Copy the code

The factory pattern

Reduced code, but still can’t identify what type the object is

 function factory(name, gae) {
   const a = {};
   a.name = name;
   a.age = age;
   return a;
 }
Copy the code

The constructor

The function starts with a capital letter and, unlike factory mode, does not have to display the create and return objects. This refers to the instance object that comes out of new. But create a new method with the same functionality for each instance

 function Factory(name, gae) {
   this.name = name;
   this.age = age;
   this.say = () => {
     return this.name;
   }
 }
 
 const fact1 =  new Factory();
 const fact2 = new Factory();
 fact1.say === fact2.say // false
Copy the code

Extend the implementation of new

All new does is create and return objects, changing this to point to. This refers to the article execution context for detailed explanation

function myNew() {
   const obj = {};
   Factory.apply(obj,[...arguments]);
   obj._proto_ =  Factory.prototype;
   return obj;
}
Copy the code

Prototype mode

Properties and functions with the same name on the instance can override those on the stereotype

Each function has a Prototype object to which the _proto_ attribute of the new instance points and the constructor attribute of the Prototype object points

 function Factory() {
 }
 Factory.prototype.name = 'jack';
 Factory.prototype.say = () => {
     return this.name;
 }
 const fact1 =  new Factory();
 fact1.name = 'hahhah';
 const fact2 = new Factory();
 fact1.say === fact2.say // true
 
Copy the code

Do not assign directly to the prototype object; you lose its constructor

Extended prototype chain

All the way down the _proto_ built-in property of an instance object to NULL is the prototype chain for that object

Constructor and prototype composition

Instance properties are defined in constructors, while the properties constructor and methods shared by all instances are defined in stereotypes.

 function Factory(name, gae) {
   this.name = name;
   this.age = age;
 }
 Factory.prototype.say = () => {
     return this.name;
 }
Copy the code

inheritance

Combinatorial inheritance of constructors and prototype chains

The constructor of the parent class is called twice

Function Parent(name) {this.name = name this.colors = [' red ', 'blue', 'green']} Parent. Prototype. SayName = function () {console.log(this.name)} function Child(name, Job) {// Inherit attributes Parent. Call (this, Name). This job = job} / / Child inherit method. The prototype = new Parent () the Child. The prototype. The constructor = the ParentCopy the code

Parasitic combinatorial inheritance

The object.create () method creates a new Object, using an existing Object to provide the _proto_ of the newly created Object.

function Parent(name) { this.name = name this.colors = ['red', 'blue'] } Parent.prototype.sayName = function () { console.log(this.name) } function Child(name, Job) {// Inherit attributes Parent. Call (this, Prototype = object.create (Parent. Prototype) // Fix constructor Child.prototype.constructor = Child Child.prototype.__proto__ === Parent.prototype // trueCopy the code