ecmascript-harmony

How to enable harmony syntax support in coffeescript?

妖精的绣舞 提交于 2019-12-21 02:27:08
问题 I used node.js(0.11.13) with --harmony flag and used function *() and yield keywords. I tried to simplify my development on node.js with help of coffeescript, so far it works great but I went into troubles with yield and declaring a generator - it complains about 'reserved keyword yield' . Any ideas? 回答1: Another way to open the gate to the black dimension is: co = require 'co' sleep = require 'co-sleep' co(`function*(){1` console.log 'hi!' `yield sleep(1000)` console.log 'bye!' `1}`)() It's

ES5 Object.assign equivalent

为君一笑 提交于 2019-12-20 08:49:02
问题 I wanted to do something which is very straight-forward using Object.assign . var firstObj = {name : "Saba H.", rollNo : 1}; var secondObj = {college : "WCE"}; var wholeObj = Object.assign(firstObj, secondObj); console.log(wholeObj); // {name : "Saba H.", rollNo : 1, college : "WCE"} As Object.assign is part of ECMAScript6 harmony proposal and not supported across many browsers, is it possible to do with ES5? If not then is there any micro library? 回答1: In underscore.js you can use like, _

ES6 Generators- Example where there is no yield expression for the first next()

半腔热情 提交于 2019-12-19 03:59:39
问题 For ES6 generators, why does the author of this blog post say: from: http://davidwalsh.name/es6-generators "The first next(..) call, we don't send in anything. Why? Because there's no yield expression to receive what we pass in." Doesn't the first it.next() call (yield (x + 1)) ? function *foo(x) { var y = 2 * (yield (x + 1)); var z = yield (y / 3); return (x + y + z); } var it = foo( 5 ); // note: not sending anything into `next()` here console.log( it.next() ); // { value:6, done:false }

What does ReturnIfAbrupt mean in ES6 draft?

你离开我真会死。 提交于 2019-12-18 15:46:08
问题 I'm currently implementing some shims for the ES6 draft. I'm wondering if anyone can tell me what ReturnIfAbrupt means. For instance, my implementation for Number.toInt (which calls internal [[ToInteger]] is as follows: if (!('toInt' in Number)) Object.defineProperty(Number, 'toInt', { value: function toInt(value) { // ECMA-262 Ed. 6, 9-27-12. 9.1.4 // 1. Let number be the result of calling ToNumber on the input argument. var number = Number(value); // 2. ReturnIfAbrupt(number). // ? // 3. If

ES6 Modules vs. HTML Imports

纵饮孤独 提交于 2019-12-18 12:26:14
问题 HTML Imports are a part of the Web Components specification and provide a way to handle dependencies on the Web. ES6 modules also do the same thing, but just for Javascript code. Is there is any clarity on how these two will work together? Edit: An example: On a recent project of mine, I had two Javascript components (files) one of which depended on the other, but any HTML code (which is another component) could use either of them. So when I included the dependent script in my HTML, I did not

Getting a promise's value via yield & co

烈酒焚心 提交于 2019-12-18 04:42:22
问题 I'm trying to figure out how to get the value of a promise via yield , possibly with "co": function *(){ var someVar = yield functionThatReturnsAPromise(); } The called function is not a generator, just a normal function. With the above, someVar == Promise , but I want the resolved value. Does co or some other library have a way of doing this? 回答1: Yes, co can do that. You'll have to wrap parent function inside co call: co(function *(){ var someVar = yield functionThatReturnsAPromise(); })()

Splitting up class definition in ES 6 / Harmony

我的梦境 提交于 2019-12-18 01:16:07
问题 Suppose I have a class in one big file like this: export default class { constructor () {} methodA () {} methodB () {} methodC () {} } And I want to break up the class definition so that methodA , methodB , and methodC are each defined in their own separate files. Is this possible? 回答1: You should be able to, as class is supposed to just be syntax sugar for the usual prototype workflow: import methodOne from 'methodOne' import methodTwo from 'methodTwo' class MyClass { constructor() { } }

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

孤街醉人 提交于 2019-12-17 09:40:21
问题 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

What's the difference between ES6 Map and WeakMap?

馋奶兔 提交于 2019-12-17 07:04:29
问题 Looking this and this MDN pages it seems like the only difference between Maps and WeakMaps is a missing "size" property for WeakMaps. But is this true? What's the difference between them? 回答1: From the very same page, section "Why Weak Map?": The experienced JavaScript programmer will notice that this API could be implemented in JavaScript with two arrays (one for keys, one for values) shared by the 4 API methods. Such an implementation would have two main inconveniences. The first one is an

How to convert Set to Array?

别等时光非礼了梦想. 提交于 2019-12-17 05:21:12
问题 Set seems like a nice way to create Arrays with guaranteed unique elements, but it does not expose any good way to get properties, except for generator [Set].values, which is called in an awkward way of mySet.values.next() . This would have been ok, if you could call map and similar functions on Sets. But you cannot do that, as well. I've tried Array.from , but seems to be converting only array-like (NodeList and TypedArrays ?) objects to Array. Another try: Object.keys does not work for Sets