One last bit here on terminology. "this" has nothing to do with scope. This
is scope:
var foo = function(){
var bar = 0;
};
//bar is not available here because it is not in scope
"this" is actually pretty easily defined: "this" is the object to which the
current function belongs. Its how objects with methods can refer to
themselves (how else could they?). When a function is defined on its own,
its owner is the window:
(function(){ console.log(this) })();
//logs the window
Even if that function is inside another function or method this is the case:
var foo = {
bar: function(){
console.log(this); //logs foo
(function(){ console.log(this) })(); //logs window
}
};
We bind things so that we can use functions as arguments within an instance
and still reference that instance:
var foo = {
bar: function(){
(function(){ console.log(this) }).apply(this); //logs foo
}
};
On Wed, Feb 23, 2011 at 9:04 AM, Sean McArthur <[email protected]>wrote:
> It looks you were trying to pass scope to addEvent using a third argument,
> but it does not work that way. As Piotr showed, you need to bind the
> function.
>
>
>
> On Wed, Feb 23, 2011 at 8:41 AM, Piotr Zalewa <[email protected]> wrote:
>
>> http://jsfiddle.net/zalun/GwxvG/2/
>>
>> bind changes the scope, so this refers to the object and not to clicked
>> element.
>> If you're confused about what is "this" in a function simply
>> console.log(this) as a first thing in this function will give you a hint.
>>
>> zalun
>>
>> On 02/23/11 16:29, roark wrote:
>> > Hi Everyone.
>> >
>> > I've had a good read through the article Aaron wrote at
>> http://jqueryvsmootools.com
>> > and I'm really battling to understand scope or when to use "this" in
>> > my classes etc.
>> >
>> > I'm writing a class that will display a help context when you focus on
>> > a filed in a form.
>> > http://jsfiddle.net/roark/GwxvG/1/
>> >
>> > When I click on a filed, i get "this.function_name" is not defined.
>> > Trying to search google for the term 'mootools "this" ' is not very
>> > successful as you can image so i was hoping soemone could have a look
>> > and let me know where I'm going wrong?
>> >
>> > Thanks
>> > Roark
>>
>>
>> --
>> blog http://piotr.zalewa.info
>> jobs http://webdev.zalewa.info
>> twit http://twitter.com/zalun
>> face http://facebook.com/zaloon
>>
>
>