On Mar 10, 3:27 pm, "T.J. Crowder" <[email protected]> wrote:
> > I do agree there are just times when .bind() is called for, but I am fairly
> > certain the majority of people WAY overuse it.
>
> +1 to that, where you're defining the function inline.  Where you're
> not, while this may be faster:
>
>     var self = this;
>     handler = function() {
>         self.realHandler();
>     }
>
> ...bind is (for me) much clearer:
>
>     handler = realHandler.bind(this);
>
> But again, yeah, when you're defining the function inline _anyway_,
> take advantage of the native closure stuff.

I absolutely agree that not using `bind` with local function works
well and works fast. I also agree that `bind` is overused. What I want
to stress, though, is that when binding a non-local function, `bind`
actually *eliminates an extra closure* that would otherwise be
implicitly created. You either save on memory, or on runtime
performance:

1) Bound event handler is augmented and is slower than original one
(at runtime), as it has to go through function call, `apply`
invocation and even worse - `$A` normalization, etc. (in older
versions of prototype)

this.element.observe('click', this.onClick.bind(this));


2) Non-bound event handler is faster at run time, as it accesses
`onClick` from the proper object directly, but presumably consumes
more memory (due to extra closure, created during function expression
evaluation)

var self = this;
this.element.observe('click', function(){ self.onClick() });

[...]

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to