ecmascript-harmony

What can we do with ES6 Generator that we cannot with for loop?

試著忘記壹切 提交于 2019-11-27 05:12:14
I went trough ES6 features and Generators caught my eye. One thing that sprang to mind is chaining Promise objects, that I could not do with loops. What other mechanics we will be able to do, that we could not before? I do understand this is broad question, still I can't think of anything but Promises at the moment. By using yield , generators can be suspended at any point in the control flow of your function, saving your current state of execution (scope & stack). Without generators, this is more complicated: you need to explicitly keep track of the state branching and (especially) looping

JavaScript ES6: Test for arrow function, built-in function, regular function?

房东的猫 提交于 2019-11-27 04:24:58
Is there an elegant way to tell Harmony's slim arrow functions apart from regular functions and built-in functions? The Harmony wiki states that: Arrow functions are like built-in functions in that both lack .prototype and any [[Construct]] internal method. So new (() => {}) throws a TypeError but otherwise arrows are like functions Which means, you can test for arrow functions like: !(()=>{}).hasOwnProperty("prototype") // true !(function(){}).hasOwnProperty("prototype") // false But the test will also return true for any built-in function, e.g. setTimeout or Math.min . It sort of works in

In ES6, what happens to the arguments in the first call to an iterator's `next` method?

谁说我不能喝 提交于 2019-11-26 23:30:54
问题 If you have an generator like, function* f () { // Before stuff. let a = yield 1; let b = yield 2; return [a,b]; } And, then run var g = f(); // this question is over this value. g.next(123); // returns: { value: 1, done: false } g.next(456); // returns: { value: 2, done: false } g.next(); // returns: { value: [ 456, undefined ], done: true } The first call to .next() to set a to 123 and the second call to set b to 456 , however at the last call to .next() this is return, { value: [ 456,

Creating a regular weak-reference in Javascript using WeakMaps

不打扰是莪最后的温柔 提交于 2019-11-26 21:13:14
问题 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

Map using tuples or objects

两盒软妹~` 提交于 2019-11-26 19:46:26
问题 I'm trying to use the new (ES6) Map objects in order to represent a map between properties and a value. I have objects in a form similar to: {key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N} I want to group them based on both their key1 and key2 value. For example, I want to be able to group the following by x and y : [{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}] And obtain a Map containing: {x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4} {x:3,y:4} ==> {x:3

Is there a mechanism to loop x times in ES6 (ECMAScript 6) without mutable variables?

喜夏-厌秋 提交于 2019-11-26 18:14:04
The typical way to loop x times in JavaScript is: for (var i = 0; i < x; i++) doStuff(i); But I don't want to use the ++ operator or have any mutable variables at all. So is there a way, in ES6, to loop x times another way? I love Ruby's mechanism: x.times do |i| do_stuff(i) end Anything similar in JavaScript/ES6? I could kind of cheat and make my own generator: function* times(x) { for (var i = 0; i < x; i++) yield i; } for (var i of times(5)) { console.log(i); } Of course I'm still using i++ . At least it's out of sight :), but I'm hoping there's a better mechanism in ES6. OK! The code below

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

谁说我不能喝 提交于 2019-11-26 15:07:27
问题 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

SyntaxError: Unexpected Identifier (Generators in ES6)

孤街醉人 提交于 2019-11-26 14:08: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

JavaScript double colon (bind operator)

别等时光非礼了梦想. 提交于 2019-11-26 12:10:56
As you know, there is a proposal for a shortcut for .bind() function, so you can write: ::this.handleStuff and it will work like that in es5: this.handleStuff.bind(this) My question is: will it be possible to pass arguments this way? I mean a way of writing this with the aforementioned shortcut: this.handleStuff.bind(this, 'stuff') It's a pretty common pattern in React, so it would be nice to shorten it a little. Bergi No. The bind operator ( spec proposal ) comes in two flavours: Method extraction ::obj.method ≡ obj.method.bind(obj) "virtual method" calls obj::function ≡ function.bind(obj)

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

孤街醉人 提交于 2019-11-26 11:30:41
问题 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