The constructor
There are three ways to create objects in JS
- Literals create objects
var obj = { }
- Use the new keyword to create an object
var obj = new Object()
- Use constructors to create objects
Function Obj(name,age){this.name = name; This.age = age} var dd = new Obj(' WSJ ',18) console.log(dd.name) Can only be called by constructors obj. gender = 'man' console.log(obj.gender)Copy the code
Constructors create a memory waste that can be solved with prototype
The prototype
Each constructor has a Prototype property that points to another object, so we can store the same method on the prototype object to avoid wasting memory. And this allows all instances of objects to share these methods.
How do I access instance objects?
- Constructor. Prototype
- Object instance. Proto
The properties we create can be accessed either by the constructor or by the instantiated object, but the methods and properties we create in the constructor’s Prototype object can be accessed by all objects.
Constructor constructor
There is a constructor property in the object stereotype, pointing to the constructor itself
Prototype chain
The prototype chain I understand is a relationship in which an Object’s __proto__ points to its prototype Object, which is also an Object and has its own prototype Object, Object