One, using constructors to implement "inheritance
Reference: JavaScript Object-oriented Programming (II): Inheritance of constructors
function extend (child, Parent) {
var F = function () {}; Using an empty object as a mediation F is an empty object, so it hardly accounts for memory.
F.prototype = Parent.prototype;
Child.prototype = new F ();
Child.prototype.constructor = child; Does not affect the parent
Child.uber = Parent.prototype; Spare
}
Second, the inheritance of the non-constructor function
Reference: JavaScript Object-oriented programming (c): Inheritance of non-constructors
Shallow copy
function Extendcopy (p) {
var c = {};
for (var i in P) {
C[i] = P[i];
}
C.uber = p;
return C;
}
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;
}
JS Implementation inheritance