optimistx wrote:
<script type="text/javascript">
function BigComputer(answer) {
this.the_answer = answer;
this.ask_question = function () {
alert(this.the_answer);
}
}
function addhandler() {
var deep_thought = new BigComputer(42),
the_button = document.getElementById('thebutton');
the_button.onclick = deep_thought.ask_question;
}
window.onload = addhandler;
</script>
How is that supposed to work? What is 'this' here and why? Do we
get back the answer '42'? The answer is in the link, but I would
be curious to know how many of the readers could in a minute or
two find the correct answer AND the explanation without looking
at the link. (my guess is: 1 or 2

)
You would be wrong. If you look in the group's archives you may see that
issues surrounding the value of - this - in this and similar/related
contexts are quite often the subject of questions to the group, and have
received detailed (and factual) answers from many individual over the
years. Making it certain that the individuals giving those answers
understood the situation, and very likely than the majority of the
individuals reading those answers either already knew or now know the
expected behaviour.
Of course searching the newsgroup for "this" will not be particularly
profitable, but searching for "this keyword" and/or "this operator" will
very likely turn up those answers (strictly - this - is a keyword not an
operator, but strongly insisting on that distinction is a bit too
pedantic).
My two brain cells got twisted like spaghetti with the
solution:
Function.prototype.bind = function(obj) {
var method = this,
temp = function() {
return method.apply(obj, arguments);
};
return temp;
}
function addhandler() {
var deep_thought = new BigComputer(42),
the_button = document.getElementById('thebutton');
the_button.onclick = deep_thought.ask_question.bind(deep_thought);
}
If I have to write and read code like this with a speed 60
lines/hour I have a lot to learn. Good material for learning?
That is not an ideal solution in the context of intrinsic event handlers
because it complicates the process of re-retrieving the reference to -
the_button -, which is often wanted in that sort of context (you either
have to store it as a property of the object (introducing the circular
reference memory leak issue on IE), re-use - getElementById - (and so
hard code or store the identifier in the called method or on the object)
or retrieve it from the event's target (using test-and-branch code to
account for browser differences), when in the intrinsic event listener
call the - this - value would have been a reference to that element).
It also suffers from encouraging a very inefficient style of coding
where the association of the object method and the object is established
just prior to the use of function and re-established for each use (this
is more noticeable with - setTimeout than intrinsic event handlers)
rather than one time only when the object is created. The proposed -
bind - method creates a new function object instance each time it is
called and that is an overhead that may not need to be suffered more
than once.
Richard.