Prototype-related concepts
- About object-oriented concepts
- Class: In JS is the constructor function
- Instances (instance) and objects (object)
- An instance typically refers to an object created by a constructor. We become an example of XXX constructors
- An instance is an object. The object is a general term.
- Instances and objects are a synonym
- Key-value pairs and properties and methods
- The set of key-value pairs in JS is called an object
- If the value is data (not a function), the key-value pair is called the property
- If the value is a function (method), the key value pair is called the method
- Parent class and Child class
- The traditional object-oriented language uses classes to implement inheritance. Then there is the concept of a parent class, a subclass
- The parent class is also called the base class, and the subclass is also called the derived class
- In JS, it is often called the parent object, the child object. Base object, derived object.
- Prototype-related concepts
- A mysterious object is called a "prototype attribute" for a constructor function.
- The mysterious object is the prototype property of the constructor.
- Abbreviation prototype
- The mystery object is also related to the object created by the constructor.
- What's the relationship?
- The object created by the mysterious object for the constructor is called the prototype object.
- Abbreviation prototype
- Object inherits from its prototype
- constructor-created object inherits the prototype property from the constructor
- The object that the constructor creates inherits from the prototype object of the object
- The object that the constructor creates for the object that is represented by the constructor's prototype property is two different objects
- Members in the prototype that can be used directly by the instance object
- That is, the instance object directly "contains" the members of the prototype.
- So the instance object inherits from the prototype
- Such inheritance is "Prototype inheritance"
- Some questions
- What is the {} constructor?
- Every literal object has a constructor.
- {} Object
- [] Array
- /./REGEXP
- Function ... Function
How to use Prototypes
Why use prototypes?
To improve the reusability of code
Attributes are not placed on the prototype: properties represent features that are easily changed
method is placed on the prototype: The method is treated the same way, can be reused
- Using the dynamic properties of objects
- constructor. prototype.xxx = VVVV;
- Use Direct replacement
Student.prototype = { sayHello: function () {}, study: function () {} };
The concept of prototypes