BTW, dom.js [...] is going great, with David Flanagan and Donovan Preston among others hacking away on it, and (this is important) providing feedback to WebIDL’s editor, Cameron McCormack.An episode of “A Minute With Brendan Eich” provides background information on dom.js.
Construct 1: private name objects.
module name from "@name"; let key = name.create(); function MyClass(privateData) { this[key] = privateData; }Private names are hidden from mechanisms such as
Construct 2: computed keys in object literals.
const __password = Name.create(); var obj = { [__password]: "my secret", unlock: function(pwd) { if (pwd === this[__password]) { ... } } };Putting an expression in square brackets allows you to compute the name of a property in an object literal.
let square = (x) -> (x * x); // expression body let range = (start, end) { // statement body var result = []; for(; start < end; start++) { result.push(start); } return result; }; // fat arrow: use "this" of surrounding function callback = (msg) => ( this.vmail.push(msg) );
let empty = {||}; let square = {|x| x * x }; // paren-free call to mimic control structure syntax. // Therefore: no semicolon at the end. let a = [1, 2, 3]; let b = a.map { |e| e * e }; // [1, 4, 9] // Bonus: break, continue, return leave the block // (unlike in function expressions) Boolean.prototype.thenElse = function(ifTrue, ifFalse) { return this ? ifTrue() : ifFalse(); } function getElement(arr, index) { (index < 0).thenElse {|| return arr[arr.length + index]; } {|| return arr[index]; } }
I’ve been clear about preferring block-lambdas over arrows for their added semantic value and compelling syntax mimicking control statements. It’s good to hear @jashkenas [CoffeeScript creator Jeremy Ashkenas] agree in effect.I agree with Eich’s choice. When used as an argument for a function, a block lambda has lexical “this” – this in a block lambda refers to the object on which the surrounding method has been invoked. That will eliminate a frequent source of errors that sometimes even tricks experts. With block lambdas, JavaScript newbies will automatically do the right thing.[...] JS syntax is not a problem for some (perhaps many) users. For others, it’s a hardship. Adding block-lambdas helps that cohort, adds value for code generators (see Harmony Goals above), and on balance improves the language in my view. It won my straw show-of-hands poll at CapitolJS against all of arrows, vaguer alternatives, and doing nothing.
Still, I think we should react to valid complaints about JS, whatever the source.Some of the complaints, with reactions:
This would have to affect Number and Math, but lexically — no dynamic scope. Still a bit hairy, and not yet on the boards for Harmony. But perhaps it ought to be.I don’t find this solution particularly intuitive. Maybe one could use an object-oriented solution that is compiled to something more efficient.
var a = new BigNum(344); var b = BigNum.parse(inputString); var c = a.times(b);The above is a bit like the pragma, but allows one to mix different arithmetics.