How to add a method to a javascript object? -
this question has answer here:
- javascript add method object 4 answers
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
Post a Comment