This (below) ended up giving me a "maximum call stack size exceeded" error. It seems like it's due to the way "this" is being interpreted within the "this.actions" object. Within that object, does "this" refer to that object, or the instance of the Unit class? If the former, would putting a .bind(this) on the end of the "this.actions" object make "this" refer to the class instance instead? If so, why? If not, why not?
function Unit(){
this.move = function(direction){
switch(direction){
case 'up': { console.log('foo'); break; }
case 'down': { console.log('foooo'); break; }
}
console.log('bar');
}
this.shoot = function(){console.log('zap')}
this.actions = {
'moveUp' : function(){ this.move('up') },
'moveDown' : function(){ this.move('down') },
'shoot' : function(){ this.shoot() }
}
return this
}
move
to be method of object. better make it simple function and call it like'moveUp' : move.call(this, 'up') },
same withshoot