- Edited
I'm just starting to deepen my knowledge on that topic. I'm was just reading this article (and I'm not understanding all of it):
http://alistapart.com/article/getoutbindingsituations
(referred from this page:)
http://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this
So I'm less ignorant than before, but still not as enlightened as I'd like to.
I've run into some "this" issues before, and I usually solve them by explicitly copying the value of this to a variable - it also avoids ambiguities... example:
Another thing that can help is strict mode.
That's usually the type trouble I may run into and how I solve it. I guess I don't do a lot of passing around function pointers.
But I'd like to understand how it all works, and have more insight on the topic, and as I research it, I'm wondering if there are any JavaScript expert here who have a say on the topic...
So, does a JavaScript function simply inherit the context within which it's called? Or is there more to it?
http://alistapart.com/article/getoutbindingsituations
(referred from this page:)
http://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this
So I'm less ignorant than before, but still not as enlightened as I'd like to.
I've run into some "this" issues before, and I usually solve them by explicitly copying the value of this to a variable - it also avoids ambiguities... example:
function myClass() {
var myClassContext = this;
this.someProperty = "nice";
this.myMethod = function(arg) {
// some code
someElem.onclick = function(el) {
el.setAttribute('class', myClassContext.someProperty);
}
}
}
So, here I assigned the value of this to myClassProperty, and I can further use it without running into ambiguous situations or errors.Another thing that can help is strict mode.
That's usually the type trouble I may run into and how I solve it. I guess I don't do a lot of passing around function pointers.
But I'd like to understand how it all works, and have more insight on the topic, and as I research it, I'm wondering if there are any JavaScript expert here who have a say on the topic...
So, does a JavaScript function simply inherit the context within which it's called? Or is there more to it?