javascript - Module pattern and this -


i using module pattern javascript "classes". there significant downside declaring var self outisde of class returning , setting this inside class constructor don't have worry context switching when don't want to. in small example it's unnecessary, example.

example:

    var seat = (function() {       var self = null;       function seat(startx, starty, inputseatnumber, inputtablenumber) {         self = this;         self.radius = 10;         self.x = startx; self.y = starty;         self.seatnumber = inputseatnumber;         self.tablenumber = inputtablenumber;       }        seat.prototype.moveto = function(newx, newy) {         if(newx >= 0 && newy >= 0) {           self.x = newx; self.y = newy;         }       };        return seat;     })(); 

edit: example added

var seatingchartview = (function() {   function seatingchartview(canvas_id, seatingchartcontroller, seatform) {     this.stage = new createjs.stage(canvas_id);     this.controller = seatingchartcontroller;     this.seatform = seatform;      this.disablerightclick(canvas_id);   }    seatingchartview.prototype.render = function() {     this.stage.update();   }    seatingchartview.prototype.addseat = function(newseat) {     var newcircle = new createjs.shape();     newcircle.graphics.beginfill("black").drawcircle(0, 0, 10);     newcircle.x = newseat.x;     newcircle.y = newseat.y;     newcircle.seat = newseat;     newcircle.on('click', function(event) {       if(event.nativeevent.button == 2) {         this.seatform.open(event.currenttarget.seat);       }     });     newcircle.on('pressmove', this.controller.moveseat)     this.stage.addchild(newcircle);   }    seatingchartview.prototype.removeseat = function(seat) {     this.stage.children.foreach(function(child) {       if(child.seat === seat) {         this.stage.removechild(child);       }     });   }    seatingchartview.prototype.setbackground = function(imagelocation) {     this.background = new createjs.bitmap(imagelocation);     window.settimeout(function() {       this.stage.canvas.width = this.background.image.width;       this.stage.canvas.height = this.background.image.height;       this.stage.addchild(this.background);       this.stage.update();     }.bind(this), 500);   }    seatingchartview.prototype.disablerightclick = function(canvas_id) {     $(function() {       $('#' + canvas_id).bind('contextmenu', function(e) {         return false;       });     });   }  return seatingchartview; })(); 

in case every new instance of seat share newest self object since set in constructor. should avoid doing this.


Popular posts from this blog