javascript - Why, in Apple's Safari Developer Library, do they use anonymous functions when attaching eventsListeners? -
all,
i'm pretty new javascript development, stupid question.
but, i'm reading apple's safari developer library, , notice use "anonymous functions" when attaching eventlisteners:
this.element.addeventlistener('touchstart', function(e) { return self.ontouchstart(e) }, false);
is different this:
this.element.addeventlistener('touchstart', ontouchstart, false);
assuming different, how? if it's not, why use method?
here's link document i'm referring to:
safari developer guide - interactive visual effects
some clarification...
i understand benefit of this:
this.element.addeventlistener('touchstart', function(e) { alert("touched")}, false);
over this...
this.element.addeventlistener('touchstart', ontouchstart, false); function ontouchstart(e) { alert("touched"); }
in case, you've saved creating "permanent", named function.
but, in apple's case, function named "ontouchstart" permanent, named function either way. so, there still benefit?
thanks in advance!
your way works long don't need access members of "self" object, or supply other parameters. generally, do, though, technique use creates closure allow access local variables (such "self", typically reference "this" in calling function)
Comments
Post a Comment