How to add a method to a javascript object? -


this question has answer here:

i have js constructor:

function myobj(){     this.type = "triangle"; } 

how add method like:

triangle.mymethod(); 

you can use prototypes:

// standard canvas object     function canvasobj(canvas, x, y, w, h, fill){         this.x = x||0;         this.y = y||0;         this.w = w||0;         this.h = h||0;         this.objects = [];         this.ctx =  context_for(canvas);         this.init(canvas, fill);     };  // initial set-up of canvas     canvasobj.prototype.init = function(canvas, fill){         position_element(canvas, this.x, this.y);         resize_canvas(canvas, this.w, this.h);         draw_rect(this.ctx, 0, 0, this.w, this.h, fill);     }; 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -