I'm trying to build a simple application where I'll be creating multiple instances of a Box object which will control and manipulate its own data. However, I'm having trouble figuring out how to create global variables for use within each individual object and it's associated prototypes...
For example, I tried to make a reference to itself...
function Box( boxData ) {
// References the Box instance as I want.
console.log( this );
// I need to access this later...
var boxName = boxData.name;
var canvas = $( '#rm-' + boxData.id ).find( 'canvas' )[0];
$( canvas ).on( 'mousedown', this.onMouseDownHandler );
}
Box.prototype.onMouseClickHandler = function( event ) {
// 'boxName' is undefined as 'this' references 'event.target'
console.log( this.boxName );
}
Keep in mind that it can't act as a singleton as I'll have multiple instances of it at any one point in time.
Edit:
I'm adding the event listener in the constructor with, updated the above code.