javascript - JS several functions via prototype -
i'm new js. if want assign example 2 functions contructor, have call function declaration via prototype twice?
function shape(x, y) { this.x= x; this.y= y; } shape.prototype.foo= function() { return ...; }; shape.prototype.bar= function() { return ...; };
you can way, or can assign new object prototype (overwriting existing properties / methods):
shape.prototype = { foo : function(){ }, bar : function(){ } }; if you're adding lots of methods different prototypes , don't want overwrite entire prototype object, define helper method asignment you:
function addtoprototype(constructor, obj){ (var prop in obj){ constructor.prototype[prop] = obj[prop]; } } addtoprototype(shape, { foo : function(){ }, bar : function(){ } }); addtoprototype(shape, { : function(){ } }); addtoprototype(polygon, { somethingelse : function(){ } });
Comments
Post a Comment