javascript - How to get the object's class name when it is an instance of a subclass? -
suppose have abstract class animal:
function animal(){}; animal.prototype.communicate = function(metadata){ //abstract };
and have duck class:
function duck(){}; duck.prototype = new animal(); duck.prototype.communicate = function(metadata){ console.log("cuack!"); };
so, i'm writting greasemonkey script , need save animals in localstorage retrieve them next time page loaded (this way allows me re-create same instances had before reaload page).
so, need classname of every animal created. read similar questions , way classname is:
var aduck = new duck(); console.log("classname: " + aduck.constructor.name);
but when "classname: animal" on console. p/d: can't change programming style prototype, requirement... :(
any suggestions? time!
duck.prototype = new animal();
this code wrong; correct way prototypal inheritance little more complicated.
if can't change directly, can still fix in greasemonkey:
duck.prototype.constructor = duck;
you'll need each class.
Comments
Post a Comment