javascript - How can I use one function for different JS classes? -
assume have 2 classes classa
, classb
:
function classa(){ this.x = 1; this.y = 'a'; this.z = false; }
-
function classb(){ this.x = 0; this.y = 'b'; this.z = true; }
and want use 1 function these 2 prototype:
var foo = function(){ if(this.z){ window.alert(this.y + this.x); } }
is safe use like
classa.prototype.foo = foo; classb.prototype.foo = foo;
because assign same function object different classes.
or there way define interface (like java) , use inheritance somehow?
is safe use like...
yes
this fine. unlike python methods, javascript functions know nothing of class belong to.
Comments
Post a Comment