A shorthand for Object.defineProperty?

Axel Rauschmayer axel at rauschma.de
Tue Jun 21 02:18:32 PDT 2011


>>>> Do we really need the dynamic this arrow ->, or can we make do with just the lexical this arrow =>.
>> Are functions that depend on dynamic |this| ever *not* methods?
> 
> Sure. The main use-case is constructors (and you can make a function do double-duty, as a constructor or a function called without 'new').

With classes, constructors will become methods, right?

> Also, some times, people write functions not used as methods but called via .call or .apply. It happens.

Wouldn’t it be better to have lexical |this| in these cases, so that foo.call(null, ...) does not affect |this|? But I see what you mean, you cannot use normal .bind(), because of the way it handles the arguments that the bound function receives. Maybe use something like the following to implement =>, then?

Function.prototype.bindOnlyThis = function (myThis) {
    var that = this;
    return function() {
        return that.apply(myThis, arguments);
    };
}

>> Wouldn’t you always want to use an object literal for methods, especially if some features (such as |super|) depend on it?
> 
> Probably. But so what?
> 
> We're not making mandatory syntax either way with function, so why should we with arrow?

Old-style functions would be rarely used and there would be a clear separation of concerns:
- Want a method? Use an object literal.
- Want a function that is not a method (i.e., lexical this)? Use an arrow function with =>

This would make things easier to understand. When I explain this aspect of JavaScript to others, they are often stumped, but I have never encountered anyone who was confused by Python:
class MyClass:
    def mymethod(self):
        return "hello world"
def myfunction():
    return "hello world"

So this is probably my real point: can we make things as un-confusing as in Python?

>> Maybe I just like the distinction introduced by block lambdas too much:
>> - dynamic this --> existing functions and methods
>> - lexical this --> new, more compact construct, mainly used as the argument of functions and methods.
> 
> Block-lambdas are not arrows. Arrows are just syntax and try to shorten function usage in general (including the .bind or var self=this; idiom). Block-lambdas must preserve Tennent's Correspondence Principle, so they have no choice but to treat |this| lexically.

I know. But block-lambdas enforce a separation of concerns (because they have no choice, really) that I would like short-syntax functions (arrows or not) to enforce, as well.

>> This distinction works well as a rule of thumb and keeps things easy to explain.
> 
> It's not that simple: existing functions can and do use various binding hacks to make |this| or a substitute "lexical". Anyway, functions have |this| parameters, whether created by old function or new arrow syntax.

Right. That could be a problem, the abstraction I have in mind might become leaky at some point.

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110621/1cd50998/attachment.html>


More information about the es-discuss mailing list