Short answer: do
$('.personName').mouseover(function(event) {
obj.highlight($(event.target).attr('faceIndex'));
});
Longer explanation:
Javascript doesn't really have a concept of this
. At least not in the way you're used to thinking of it. Oh there's a keyword alright, and it kind of does the thing you expect a lot of the times but it doesn't work the way that you probably think.
The fact of the matter is that in javascipt, this
is no different than any other parameter. Let me show you.
Most people are aware that in javascript you can invoke functions like this doSomething(param1, param2)
or like this doSomething.call(null, param1, param2)
. If you wanted, you can write all function invocations using .call
See that null
there? Anything you pass in there is what this
gets set to.
doSomething.call(null, param1, param2);
doSomething.call(obj, param1, param2);
doSomething.call(window, param1, param2);
doSomething.call("foobar", param1, param2);
If you don't use .call
the runtime just takes a guess at what value you want there.
So given this, consider that the only difference between this
and any other parameter is that you don't get to give this
a name! Your problem is that you have two function scopes and the inner one has a variable named this
which hides the outer one's this
.
Solution: don't use this
. Most libraries in fact (jquery included), don't force you to use this
and also pass in the value as a regular parameter
$('.personName').mouseover(function(event) {
obj.highlight($(event.target).attr('faceIndex'));
});
ambiguity solved!
Avoid using this
in JavaScript, if at all possible. It is almost never necessary.