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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -