ecmascript-harmony

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

☆樱花仙子☆ 提交于 2019-11-26 11:29:22
问题 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. 回答1: 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,

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

人走茶凉 提交于 2019-11-26 11:10:03
问题 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

How can I clone a JavaScript object except for one key?

大兔子大兔子 提交于 2019-11-26 07:56:09
问题 I have a flat JS object: {a: 1, b: 2, c: 3, ..., z:26} I want to clone the object except for one element: {a: 1, c: 3, ..., z:26} What\'s the easiest way to do this (preferring to use es6/7 if possible)? 回答1: If you use Babel you can use the following syntax to copy property b from x into variable b and then copy rest of properties into variable y : let x = {a: 1, b: 2, c: 3, z:26}; let {b, ...y} = x; and it will be transpiled into: "use strict"; function _objectWithoutProperties(obj, keys) {

Function parameter definitions in ES6

最后都变了- 提交于 2019-11-26 07:46:14
问题 I\'m sure that this is relatively straightforward and that I\'m missing something obvious. I\'m reading through Mozilla\'s tutorials on ES6, and their chapter on destructuring contains the following pattern: FUNCTION PARAMETER DEFINITIONS As developers, we can often expose more ergonomic APIs by accepting a single object with multiple properties as a parameter instead of forcing our API consumers to remember the order of many individual parameters. We can use destructuring to avoid repeating

What is the motivation for bringing Symbols to ES6?

微笑、不失礼 提交于 2019-11-26 03:22:03
问题 UPDATE : Recently a brilliant article from Mozilla came up. Read it if you\'re curious. As you may know they are planning to include new Symbol primitive type in ECMAScript 6 (not to mention some other crazy stuff). I always thought that the :symbol notion in Ruby is needless; we could easily use plain strings instead, like we do in JavaScript. And now they decide to complicate things in JS with that. I don\'t understand the motivation. Could someone explain to me whether we really need

How to customize object equality for JavaScript Set

▼魔方 西西 提交于 2019-11-26 01:34:00
问题 New ES 6 (Harmony) introduces new Set object. Identity algorithm used by Set is similar to === operator and so not much suitable for comparing objects: var set = new Set(); set.add({a:1}); set.add({a:1}); console.log([...set.values()]); // Array [ Object, Object ] How to customize equality for Set objects in order to do deep object comparison? Is there anything like Java equals(Object) ? 回答1: The ES6 Set object does not have any compare methods or custom compare extensibility. The .has() ,

One-liner to take some properties from object in ES 6

馋奶兔 提交于 2019-11-25 22:15:56
问题 How one can write a function, which takes only few attributes in most-compact way in ES6? I\'ve came up with solution using destructuring + simplified object literal, but I don\'t like that list of fields is repeated in the code. Is there an even slimmer solution? (v) => { let { id, title } = v; return { id, title }; } 回答1: Here's something slimmer, although it doesn't avoid repeating the list of fields. It uses "parameter destructuring" to avoid the need for the v parameter. ({id, title}) =>

Methods in ES6 objects: using arrow functions

馋奶兔 提交于 2019-11-25 22:14:46
问题 In ES6, both of these are legal: var chopper = { owner: \'Zed\', getOwner: function() { return this.owner; } }; and, as shorthand: var chopper = { owner: \'Zed\', getOwner() { return this.owner; } } Is it possible to use the new arrow functions as well? In trying something like var chopper = { owner: \'John\', getOwner: () => { return this.owner; } }; or var chopper = { owner: \'John\', getOwner: () => (this.owner) }; I get a error messages suggesting that the method does not have access to

When should I use Arrow functions in ECMAScript 6?

独自空忆成欢 提交于 2019-11-25 21:41:37
问题 The question is directed at people who have thought about code style in the context of the upcoming ECMAScript 6 (Harmony) and who have already worked with the language. With () => {} and function () {} we are getting two very similar ways to write functions in ES6. In other languages lambda functions often distinguish themselves by being anonymous, but in ECMAScript any function can be anonymous. Each of the two types have unique usage domains (namely when this needs to either be bound

What is the motivation for bringing Symbols to ES6?

自作多情 提交于 2019-11-25 20:14:53
UPDATE : Recently a brilliant article from Mozilla came up. Read it if you're curious. As you may know they are planning to include new Symbol primitive type in ECMAScript 6 (not to mention some other crazy stuff). I always thought that the :symbol notion in Ruby is needless; we could easily use plain strings instead, like we do in JavaScript. And now they decide to complicate things in JS with that. I don't understand the motivation. Could someone explain to me whether we really need symbols in JavaScript? The original motivation for introducing symbols to Javascript was to enable private