ecmascript-harmony

Creating a regular weak-reference in Javascript using WeakMaps

旧街凉风 提交于 2019-11-27 22:52:52
I am trying to do the obvious thing with WeakMaps: I want to create a weak reference. In particular, I want to have a list of event-listeners without that list influencing the life of the listener. So I was very excited to find WeakMaps, until I saw they were only built to satisfy one (fairly rare) use-case, extending objects that were otherwise sealed. I can't think when I ever wanted to do that, but I need lists of listeners all the time. Is this possible to use WeakMaps in some clever way I haven't thought of to do this? Bergi No, it is impossible to use WeakMaps to create a weak reference.

Enable Harmony Proxies in nodejs

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 21:59:15
Is it possible to enable EcmaScript 6 Harmony Proxies in nodejs? If so, what are the pros and cons? And is there any documentation on how to use them? Thanks ! Invoking node with node --harmony-proxies should do the trick. Pros: proxies are a very powerful feature when you really need them. Cons: proxies are a much too powerful feature when you don't need them (which should be most of the time). Also, the implementation should still be regarded experimental. As for documentation, all there really is atm is the Harmony wiki, in particular this page, which reflects the current implementation of

Immediate function using JavaScript ES6 arrow functions

隐身守侯 提交于 2019-11-27 12:20:45
Does anyone know how to write an immediate function using ES6 arrow syntax? Here's the ES3/5 way of doing it: (function () { //... }()); I've tried the following but get an unexpected token error on the last line. (() => { //... }()); You can test this here: http://www.es6fiddle.net/hsb8bgu4/ From the Arrow functions examples , (() => "foobar")() // returns "foobar" So, the function invocation operator should be outside. (() => { //... })(); Sample: http://www.es6fiddle.net/hsb8s1sj/ Here is my demo codes! Always remember that function_name + () === function_caller /* ES5 */ // normal function

functional way to iterate over range (ES6/7) [duplicate]

守給你的承諾、 提交于 2019-11-27 10:25:10
This question already has an answer here: Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables? 15 answers What is the best way to do the below in more functional way (with ES6/ES7) let cols = []; for (let i =0; i <= 7; i++) { cols.push(i * i); } return cols; I tried like, return [ ...7 ].map(i => { return i * i; }); but that translated to [].concat(7).map(function (n) { return n * n; }); which is not what I expected. EDIT: @pavlo. Indeed, that was a mistake. I was using JSX, and for example, I want 7 divs, (untested) let cols = []; for (let i =0; i <= 7; i++) {

ECMAScript:Harmony / ES6 to JavaScript compiler

守給你的承諾、 提交于 2019-11-27 09:19:57
问题 After reading Peter's article on JavaScript I noticed Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers. There are currently two popular compilers with some vague ES:Harmony compliance: Traceur CoffeeScript Although CoffeeScript has some compliance it's not designed to be an ES:Harmony compiler so it's not useful to this end. Tracuer seems to be sticking more rigorously to the ES:Harmony specification but I don't know whether it intends to

SyntaxError: Unexpected Identifier (Generators in ES6)

我与影子孤独终老i 提交于 2019-11-27 08:56:45
I came up with this simple experiment after reading the documentation on generators from MDN : var nodes = { type: 'root', value: [ { type: 'char', value: 'a' }, { type: 'char', value: 'b' }, { type: 'char', value: 'c' }, ], }; function* recursiveGenerator(node) { if (node.type === 'root') { node.value.forEach(function (subnode) { for (var suffix of recursiveGenerator(subnode)) { yield suffix; } }); } else { yield node.value; } } for (generated of recursiveGenerator(nodes)) { console.log(generated); } Running it on node.js v0.11.9 with the --harmony flag set produces the following error: alix

ES6 export default AssignmentExpression

非 Y 不嫁゛ 提交于 2019-11-27 08:49:55
问题 export default var foo = {...} It's AssignmentExpression and valid es6 syntax? JSHint says it Expected an identifier and instead saw 'var'. On last spec I not found any relation with VariableStatement and AssignmentExpression. 回答1: var foo = {...} is not an AssignmentExpression . AssignmentExpression is almost the top level non-terminal symbol that represents all expression, i.e. basically every expression is an AssignmentExpression . var only appears in a variable declaration or a for loop,

JavaScript native Promise() without callback

点点圈 提交于 2019-11-27 08:27:12
问题 Look at this jQuery code: var promise = new Deferred(), some; some = function(promise) { // do cool things promise.resolve(); }; promise.then(/* callback cool things */); // init everything some(promise); I am not sure about architecture correctness of such approach, but I used it for long time and it is convenient for me. In native JavaScript I can not use such approach. Constructor new Promise() requires a callback parameter, so I can not pass instance of Promise as a parameter. So my

Is it possible to reset an ECMAScript 6 generator to its initial state?

我的未来我决定 提交于 2019-11-27 08:15:00
Given the provided (very simple) generator, is it possible to return the generator back to its original state to use again? var generator = function*() { yield 1; yield 2; yield 3; }; var iterable = generator(); for (let x of iterable) { console.log(x); } // At this point, iterable is consumed. // Is there a method for moving iterable back // to the start point by only without re-calling generator(), // (or possibly by re-calling generator(), only by using prototype // or constructor methods available within the iterable object) // so the following code would work again? for (let x of iterable

user defined object equality for a set in harmony (es6)

一笑奈何 提交于 2019-11-27 05:18:14
I have a problem where I'm generating many values and need to make sure I only work with unique ones. Since I'm using node js, with the --harmony flag, and have access to harmony collections, I decided that a Set may be an option. What I'm looking for is something similar to the following example: 'use strict'; function Piece(x,y){ this.x = x this.y = y } function Board(width,height,pieces){ this.width = width this.height = height this.pieces = pieces } function generatePieces(){ return [ new Piece(0,0), new Piece(1,1) ] } //boardA and boardB are two different but equivalent boards var boardA