On Apr 3, 2012, at 2:01 AM, Claus Reinke wrote:
>
>>> I agree that leading |this| could be important for dynamic non-method
>>> use-cases, but those are relatively rare (let's not discount JQuery, but
>>> again, it could use long functions and survive). We could put
>>> leading-this-parameterization on the agenda for May, but we'll have to be
>>> careful not to lose consensus on arrows.
>> Other than the JQuery style misuse of this, what are the use cases? If you
>> want to bind this, why wouldn't a method invocation on an objet be involved?
>
> The ES array loops accept an optional this parameter to
> be used for the loop callback.
It is up to the caller of the forEach to provide both the callback function and
the optional this arg. So the caller has the option on both the kind of
function passed and whether a this value is needed. Were today someone might
code:
I assume that the most common use of the optional argument today is something
like this:
var obj = {
id: genID();
log: function(value) {this.reporter.log(value+" from "+this.id};
state: populateArray();
logAll: function() {
this.state.forEach(
function(v) {this.log(v)},
this
)}
};
in other-words, it is available as an alternative to saying:
logAll: function() {
var self = this;
this.state.forEach(function(v) {self.log(v)});
}
(Note that if the bind that log was invoked on was anything other than the
outer methods this then there would be no need for either of the above
circumventions0
which is just an idiom to get around unwanted dynamic this binding in a method.
With arrow functions you would just say:
logAll: function() {this.state.forEach(v-> ths.log(v))};
Arrow functions need to eliminate the need for such idioms that get around
unwanted dynamic this bindings.
> The new strawman for data
> parallelism also passes this to its method callbacks.
I need to look at the Rivertrail spec. but until I do, my guess, is that it is
doing something similar that probably really isn't needed once arrow functions
are available.
>
> Is there anything wrong with my getthis/fn wrappers as
> a workaround? We can't use 'this' as parameter name,
> and the wrapper adds a pair of parens. Anything else?
No, there's nothing wrong with your wrappering, but I'm not sure why it would
be needed. You code code the above as:
logAll: function() {this.state.forEach(fn((it,v)-> it.log(v)),this)};
but why would you? Particularly since you have to name the explicit initial
this-like argument in the arrow something other than "this".
It seems like the main motivaton for a leading parameter named "this" in arrow
functions is to use "this" to mean something different from the invocation
target of the current method. Do that is a really bad idea as it just creates
confusion about the the normal meaning of "this".
Allen
>
> Claus
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss