I'm a开发者_运维问答ttemping a bit of a dance to get some classical polymorphism in javascript; something like
function Inherit(base, sub) {
var baseType = typeof base;
sub.prototype = new baseType();
}
Anyone have a clue as to how to do this without having to new up base prior to passing it in?
The typeof
operator will always return a string
so that is pretty much off the table. You should be able to do something like this. Although I don't really see how this adds value to the JavaScript type system.
function Base() {
}
function Sub() {
}
function Inherit(base, sub) {
sub.prototype = new base();
}
Inherit(Base, Sub);
精彩评论