1 package
Encapsulates the property and method as an object and even generates an instance object from the prototype object
1.1 Simple package:var cat1 = {}; Create an empty object
Cat1.name = "Mao"; Assigning values according to the properties of the prototype object
Cat1.color = "Yellow";
1.2 constructor prototype mode
JavaScript specifies that each constructor has a prototype
property that points to another object. All properties and methods of this object are inherited by an instance of the constructor.
This means that we can define the invariant properties and methods directly prototype
on the object.
function Cat (name,color) {
THIS.name = name;
This.color = color;
}
Cat.prototype.type = "Cat animal";
Cat.prototype.eat = function () {alert ("Eat Mouse")};
Generate instance var cat1 = new Cat ("Da Mao", "Yellow");
2 constructor inheritance
2.1 Using Empty object inheritance:
function extend (child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F ();
Child.prototype.constructor = child;
Child.uber = Parent.prototype;
}
2.1 Copy Inheritance
function Extend2 (Child, Parent) {
var p = parent.prototype;
var c = Child.prototype;
for (var i in P) {
C[i] = P[i];
}
C.uber = p;
}
3 non-constructor inheritance (two ordinary objects)
3.1 Object () method
Function Object (Father) {
function F () {}
F.prototype = Father;
return new F ();
}
Use: var Doctor = object (Chinese);//Chinese Doctor's example
Then add the object to the native Doctor.career = ' doctor ';
3.2 Shallow Copy
function Extendcopy (p) {
var c = {};
for (var i in P) {
C[i] = P[i];
}
C.uber = p;
return C;
}
Use: var Doctor = extendcopy (Chinese);
3.3 Deep Copy
function Deepcopy (p, c) {
var c = C | | {};
for (var i in P) {
if (typeof p[i] = = = ' object ') {
C[i] = (P[i].constructor = = = = Array)? [] : {};
Deepcopy (P[i], c[i]);
} else {
C[i] = P[i];
}
}
return C;
}
Use: var Doctor = deepcopy (Chinese);
JavaScript encapsulation constructors inherit non-constructor inheritance