Possible Duplicate:
Javascript: Object Literal reference in own key’s function instead of ‘this’
I have this simple code :
var myObj = {
init: function ()
{
this.Name = "Royi";
this.BindMe();
}
BindMe: function ()
{
$('myButton').on("click", this.Work);
},
Work: function ()
{
var self = this; <--------
}
}
Running :
myObj.init();
This is a simple Object literal.
The problem is on the Work
Method. I want to make it know this
( myObj
)
there are 2 ways of doing it :
option #1
In BindMe
, When clicking , transfer the context via :
$('myButton').on("click",{self:this}, this.Work);
and in Work
do :
var self = e.data.self...
//need also to add e
option #2
write var self = myObj ;
Question
Is there any other way of doing it ?
which is the better/correct way ?