The constructor
Prior to ES6, objects were not created based on classes, but were defined with special functions called constructors and their characteristics.
You can create an object in the following three ways:
1. Object literals
var obj1 = {}
2.new Object()
var obj1 = new Object()
3. Custom constructors
function Student(name, Age) {this.name = name this.age = age this.study = function() {console.log(this.name + 'learning ')}}Copy the code
The difference between a constructor and a normal function
1. Different writing styles. Constructors usually start with uppercase letters.
The constructor is used to create a new instance object.
Constructors need the new keyword to be called
The constructor has the same function name as the class name.
Internally we use this to build properties and methods. The constructor’s this refers to the object instance itself
6, the new constructor execution flow
-
- Create an empty object
-
- Sets the __proto__ attribute inheritance for empty objects
Constructor
The Prototype property ofInheritance constructor
On the prototype objectPublic properties and methods
- Sets the __proto__ attribute inheritance for empty objects
-
- Call the constructor, replacing this in the constructor with this in the empty object, inheriting the properties in the constructor
-
- Returns a new object inside the function
// A concise implementation of the new operator
function new(constructor) { var obj = {}; // create an empty object obj obj.__proto__ = constructor. Prototype; // Assign the constructor's prototype object to obj's prototype contructor.apply(obj); Return obj (); return obj (); // Step 4: Return this object}Copy the code
The disadvantage is that it wastes memory when the object is instantiated, the constructor has new memory space. Constructor prototype objects are used: prototype methods are shared by all instance objects.
Instanceof checks whether an object is an instanceof a class. If it is, return true. All objects are descendants of Object, so instanceof any Object and Object returns true
Es5 inheritance
Two classes that implement Child to inherit from Parent
function Parent() {
this.type = 'parent'
}
Parent.prototype.eat = function () {
console.log('eat')
}
function Child(name) {
this.name = name;
this.color = 'black';
}
Copy the code
Prototype inheritance
// Point the parent class to the prototype of the child class. Child.prototype = new Parent(); // All subclasses share the same stereotype, changing one will change the othersCopy the code
Tectonic inheritance
Function Child(name) {parent.call (this); } disadvantages: cannot inherit superclass archetypes, functions are in constructors, and cannot be shared by each subclass instanceCopy the code
Combination of inheritance
Function Child(name) {parent.call (this); } Child.prototype = Parent.prototype; Disadvantages: The parent and subclass archetypes are the same archetype, and there is no way to tell who constructed the subclassCopy the code
Parasitic combinatorial inheritance
On the basis of composite inheritance, a subclass inherits an object generated by a parent class stereotype. function Child(name) { Parent.call(this); } Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child } })Copy the code
Inherits the function
function inherits(child, parent) { child.super_ = parent; child.prototype = Object.create(parent.prototype, { constructor: { value: child, enumerable: // Enumerable writable: true, // writable, any different device: // The property descriptor can be changed and deleted from the corresponding object only if and if the property is configured with true, any additional information can be configured without any additional control. The default is false. }})}Copy the code
use
function Child() {
Parent.call(this);
}
inherits(Child, Parent);
Child.prototype.fun = ...
Copy the code
Es6 inheritance
Class and extends provide an intuitive way to show the inheritance relationship between Child and Parent. The key is the use of constructor and super
Constructor is mandatory in every class, and an empty constructor method is automatically added if not defined at first, which is automatically called when new is used
Super is a method written in the constructor of a subclass, and this super calls the constructor of the parent class, and this super here is also mandatory and can only be written in the constructor of a subclass
Class Parent {// contructor constructor(name) {this.name = name} // Other private methods say () {console.log(' my name is ${this.name} '); } } class Child extends Parent { constructor(name, age) { super(name); this.age = age; } getAge() {console.log(' I call ${this.name} this year ${this.age} age '); } } let p = new Child("child", 18); console.log(p); p.say(); p.getAge();Copy the code