开发者

Invoke a constructor with the help of typeof

开发者 https://www.devze.com 2023-01-23 13:42 出处:网络
I\'m a开发者_运维问答ttemping a bit of a dance to get some classical polymorphism in javascript; something like

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

精彩评论

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

关注公众号