U
User1014
I'm a javascript noob and have a question concerning some code I've come
across that I'm trying to understand.
Here is a snippet of the relevant section:
(snip)
var closure = this;
var xhr = new Ajax.Request(this.Url,
{
method: 'post',
onComplete: function(ajaxResponse) { closure.doStuff(ajaxResponse); }
});
(snip)
When you create a function inside a function like this I know a closure
is created but what I don't understand is how this closure is used in
the above example. I've only seen examples where a reference to the
first function is returned so that it can be invoked again at a later
time with the same local variables as were created initially.
However in the above a callback mechanism is used when executing
doStuff() and the function reference would appear to be a local
reference to the inner function (onComplete)
i.e. isn't a closure redundant in the above code?
What am I not understanding here?
across that I'm trying to understand.
Here is a snippet of the relevant section:
(snip)
var closure = this;
var xhr = new Ajax.Request(this.Url,
{
method: 'post',
onComplete: function(ajaxResponse) { closure.doStuff(ajaxResponse); }
});
(snip)
When you create a function inside a function like this I know a closure
is created but what I don't understand is how this closure is used in
the above example. I've only seen examples where a reference to the
first function is returned so that it can be invoked again at a later
time with the same local variables as were created initially.
However in the above a callback mechanism is used when executing
doStuff() and the function reference would appear to be a local
reference to the inner function (onComplete)
i.e. isn't a closure redundant in the above code?
What am I not understanding here?