Skip to content

Instantly share code, notes, and snippets.

@ncou
Forked from iptpv/inherit.js
Created August 17, 2017 13:03
Show Gist options
  • Save ncou/c657906ce81fe0a6ac14b2c017c584f5 to your computer and use it in GitHub Desktop.
Save ncou/c657906ce81fe0a6ac14b2c017c584f5 to your computer and use it in GitHub Desktop.
Yet another inherit library.
var extend = function(target, source) {
for (var i in source) {
if (source.hasOwnProperty(i)) {
target[i] = source[i];
}
}
return target;
};
var objectCreate = function(ParentProto, ChildProto) {
if (Object.create) {
ChildProto = Object.create(ParentProto);
} else {
var F = function(){};
F.prototype = ParentProto;
ChildProto = new F();
}
return ChildProto;
};
var inherit = function(Parent, Child, staticProp) {
Child.prototype = objectCreate(Parent.prototype, Child.prototype);
extend(Child, staticProp);
Child.prototype.constructor = Child;
Child.prototype.super = Parent;
return Child;
};
var A = function (){
this.a = 'a';
console.log(111);
};
A.prototype.aa = 'aa';
A.prototype.methodA = function() {
return this.aa;
};
var B = inherit(A, function(){
this.bc = 5;
this.super();
}, {
static: function() {
console.log(this.prototype.super());
}
});
B.prototype.methodB = function() {
return this.b;
};
var b = new B();
b.static();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment