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

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -