Sep 27 meeting notes

Claus Reinke claus.reinke at talk21.com
Fri Sep 30 14:11:50 PDT 2011


>> class Point {
>>  var x = 0;
>>  var y = 0;
>>  distance(other) {
>>    return Math.sqrt(x * other.x + y * other.y);
>>  }
>> }
>> 
>> it is very tempting to think that var x and var y are in scope. 

Would the following analogy with modules help?

    Think of 'this' as 
    - the export object of the constructor method
    - an import object of all methods.

Then one might want nice syntax for moving between local
bindings and import/export objects, without risks to lexical
scoping. Which is the same problem modules have, isn't it?

Could one use punning ({x} standing for {x=x}), destructuring, 
and returning 'this'?

    distance(other) {
        let {x,y} = this;    // import
        return Math.sqrt(x * other.x + y * other.y);
    }

    constructor(x,y) {
        return {x,y};    // export
    }

To enable further code between 'this' initialization and constructor
return, might one permit assignment to 'this'?

    constructor(x,y) {
        this = {x,y};    // partial export
        // more setup code
    } // defaults to returning 'this'

Not taking sides, just wanted to mention the analogy..

Claus
http://clausreinke.github.com/
 


More information about the es-discuss mailing list