Reputation: 3484
var some_name =
{
moving:false,
show : function ()
{
this.moving = true;
$('element').slideDown(5000, function ()
{
this.moving = false; //How to access to attribute "moving" of class some_name?
});
},
}
Question in code.
Upvotes: 2
Views: 181
Reputation: 99919
You can bind the callback function to the current context:
$('element').slideDown(5000, $.proxy(function() {
this.moving = false;
}), this); // "this" inside of the function will be this "this"
See jQuery.proxy
Alternatively you could do this:
this
is the current context, it's value depends on how the function is called. You can assign this
to a variable outside of the function, and use this variable instead:
var that = this;
$('element').slideDown(5000, function() {
that.moving = false; //use that instead of this here
});
Upvotes: 6
Reputation: 105258
In the event callbacks, this
refers to event.target
, or the element that captured the event.
You can take the advantage of closures in javascript and access the moving
attribute like this:
show : function ()
{
var moving = true;
$('element').slideDown(5000, function ()
{
moving = false;
});
},
Note, though, that this moving
will be different of the first moving
that lives in some_name
Upvotes: 0
Reputation: 15442
Use moving
instead of this.moving
(in both occurences)
Variables are bound to the context when they are used, so even inside your event callback you can access the variables above.
Upvotes: 0