-
-
Save ncou/c657906ce81fe0a6ac14b2c017c584f5 to your computer and use it in GitHub Desktop.
Yet another inherit library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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