You’re supposed to know

Object.create()

The object.create () method creates a new Object, using an existing Object to provide the __proto__ of the newly created Object. (Open the browser console to see the results.)

Object.assign()

The object.assign () method is used to assign the values of all enumerable properties from one or more source objects to the target Object. It returns the target object.

Function.prototype.bind(), function.prototype.bind (), function.prototype.bind ()

The difference between:

  • Call () and apply() execute functions directly, and bind() returns new functions that need to be called manually.
  • Call () takes a list of arguments, and apply() takes an array of arguments

Handwritten inheritance

Constructor inheritance

Cons: Methods and properties on the prototype chain cannot be inherited

function Person() { ... } person.prototype. name = 'Person' function Student() {person.call (this)... } const s1 = new Student() console.log(s1.name) // undefinedCopy the code

Prototype chain inheritance

Disadvantages: When the properties of instantiated prototype objects are reference types, the data interact

function Person() { this.arr = [1, 2, 3, 4] } function Student() { ... Const s1 = new Student() const s2 = new Student() const s1 = new Student() const s2 = new Student() s1.arr.push(5) console.log(s1.arr) // [1, 2, 3, 4, 5] console.log(s2.arr) // [1, 2, 3, 4, 5]Copy the code

Combination of inheritance

Reference documentation

⚠ ️ key:

  • When the parent function is executed, the pointing of this is problematic
  • The subfunction prototype object points to a new object that needs to be constructed
  • Fixed point to
Function Person() {... } function Student() {person.call (this) // Execute Person to point this to the current environment... } Student. The prototype = Object. The create (Person) prototype) / / use the Object. The create to create a new Object Student. The prototype. The constructor = Student / / [interfuse] function Person() {... } function Behavior() { ... } function Student() { Person.call(this) Behavior.call(this) ... } Student.prototype = Object.create(Person.prototype) Object.assign(Student.prototype, Behaviors. The prototype) / / Object. Create and Object. The assign is to generate a new Object, to avoid a reference type influence each other Student. The prototype. The constructor = Student / / fixed pointCopy the code