开发者

Invoke a constructor with the help of typeof

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜