Some basic concepts
When you create a new property on an object, the Configurable,writeable,enumerable default value on the property is true,value default undefined
False when creating a new property with Obejct.defineproperty ()
_year The preceding underscore represents a property that can only be accessed through an object method
In object methods that cannot use Object.defineproperty, you can use __definegetter__ and __definesetter__ to implement
Object.getownpropertydescriptor can only be used for instance properties
Object.keys to get all enumerable instance properties on an object
Overriding the prototype of the prototype object directly causes the constructor property not to point to the constructor, and to manually point to the constructor, the enumerable property of the constructor needs to be reset to False
The pointers in the instance point only to the prototype, not to the constructor
Modifying native object prototypes in a production program is not recommended
Memory-saving practices: constructor patterns are used to define instance properties, while prototype patterns are used to define methods and shared properties
Implementation method
Dynamic prototype mode: In the case of having the same prototype object, it is necessary to invoke the instance of a method on the prototype object, can dynamically determine whether the method exists, if it exists, then use this method, if not exist, define the method on the prototype object, other instances can also use the method in real time.
Parasitic constructor mode:
function Specialarray () { var values=new Array (); Values.push.apply (values,arguments); Values.topipedstring=function() { return This.join (', '); } return values;}
A secure constructor pattern: similar to a parasitic constructor pattern, which means that no public property is used, and its methods do not refer to the This object
Inheritance of Classes
function A () { this.aprop=true;} A.prototype.getavalue=function() { return this. Aprop;} function B () { this.bprop=false;} b.prototype=New A (); b.prototype.getbvalue=function() {return this. Bprop;}
The following code can be updated by borrowing constructor inheritance to resolve the problem with the value of the reference type in the inheritance process (that is, when the value of the reference type exists on the prototype object, the modification of the subclass affects the parent class).
function A () { this.aprop=[1,2,3];} A.prototype.getavalue=function() { return this. Aprop;} function B () { a.call (this);} b.prototype=new A (); b.prototype.getbvalue=function() {return this. Bprop;}
The above method calls two times a constructor , generates two instances of a, that is, the instance of Aprop has two parts in memory, so the final improvement is as follows:
Parasitic combined inheritance
function inherit (a, b) { var prototype=Object (a.prototype); prototype.constructor=B; B.prototype=prototype;} function A () { this.aprop=[1,2,3];} A.prototype.getavalue=function() {return this. Aprop;} function B () {A.call (this);} b.prototype=inherit ( A, b); B.prototype.getbvalue=function() {return this. Bprop;}
Classes in JavaScript (ii)