Calling a constructor using typeof

I dance to get classic polymorphism in javascript; sort of

function Inherit(base, sub) { var baseType = typeof base; sub.prototype = new baseType(); } 

Does anyone know how to do this without having a new base before its transfer?

+4
source share
1 answer

The typeof operator will always return a string , which largely does not match the table. You should be able to do something like this. Although I really don't see how this adds value to a system like JavaScript.

 function Base() { } function Sub() { } function Inherit(base, sub) { sub.prototype = new base(); } Inherit(Base, Sub); 
+1
source

All Articles