Then you really have three somewhat modest proposals:

1. optional "function".
2. braceless functions that auto-return the completion value of the
expression and exhibit TCP freakdeekness.
3. do-expressions blocks that evaluate to their completion value.

These three seem to combine to let you do all the good things that
block lambdas offer, and are pretty elegant and easy enough to explain
to new users, IMHO.  The "do" token sort of looks like it means "Do
this right here, not in a separate context".


Indeed it looks more intuitive than block lambdas (IMHO).

A variant/extension of "() do {...}" could be "do {...}" that (if not followed by a "while") will create Function() {...} that gets evaluated when retrieveing a property ([[Get]]) or when called directly, allowing new possibilities for "this" not all feasible with block lambdas or "=>" , not impacting a lot current specs, easy to understand and use, examples :

*1- this.x in prototype*

    var f=(name) {
        this.name=name;
    };

    f.prototype={
nodeName : do {return this.name}; //creates an object = Function() {return this.name} with a flag reminding that it was a "do" creation
    };

    var g=new f('test');
    //g.prototype={
    //    nodeName : do {return this.name};
    //};


    console.log(g.nodeName); //'test'
//[[Get]] returns a function=Function() {return this.name}, if the do flag is true [[Get]] does evaluate it as if g.nodeName was called (g.nodeName() --> function.[[Call]](g))
    //this is evaluated to "this.name", where "this" does refer to g

*2- closures*

    //old way
    var a={name : 'test'};

    var f=function () {
        var self=this;
        (function() {console.log(self.name) })();
    };

    f.call(a) --> 'test'

    //new way
    var a={name : 'test'};

    var f=() {
        (do {console.log(this.name)})();
    };

    f.call(a) --> 'test'

    //old way
    function writeNodes() {
      var self = this
      var writeall = function() {
        for (var n in self.nodes) {
            self.write(n);
        }
      }
      writeall();
    }

    //new way
    writeNodes() {
        var writeall = () do {
            for (var n in this.nodes) {
                this.write(n);
            }
        };
        writeall();
    }

*3- json*

    var x={
        a: 10,
        b: do {this.a},
        c: {
            d: do {return GetBase(this)}
        }
    };

    console.log(x.b);
//[[Get]] returns Function() {return this.a} and evaluate it (see above)
    //it is evaluated to "this.a" where "this" does refer to x
    //returns 10

    console.log(x.c.d);
//[[Get]] returns Function() {return GetBase(this)} and evaluates it (see above)
    //it is evaluated to GetBase(this) where "this" does refer to x.c
    //returns x

    var x={
        a: do (this.b),
        b: do {this.a}
    };

    console.log(x.b); //infinite loop...

*4- lexical this (??)*

    var test = 'aaa';
    var o = {test:'bbb'};
    o.m = do {return this.test};
    var f=o.m;

    o.m(); //bbb
    f(); //aaa

No improvement here, but if I am correct the improvement with block lambdas is not obvious too :

    var test = 'aaa';
    var o = {test:'bbb'};
    o.m = {|| return this.test};
    var f=o.m;

    o.m(); //aaa
    f(); //aaa


_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to