ecmascript-harmony

ES6 global import

有些话、适合烂在心里 提交于 2019-12-01 02:50:06
What is the best way to import some modules in all of the files of the project, so I don't have to write stuff like: import React from 'react'; import Reflux from 'reflux'; import reactMixin from 'react-mixin'; in almost every single file? The other answer covers this, but not with valid ES6, so I'm adding my own. Make a central file to import your react components, in some central react.js file export {default as React} from 'react'; export {default as Reflux} from 'reflux'; export {default as reactMixin} from 'react-mixin'; Then in the files where you need to use these three, you could do

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

柔情痞子 提交于 2019-11-30 23:34:05
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 } console.log( it.next( 12 ) ); // { value:8, done:false } console.log( it.next( 13 ) ); // { value:42,

Arrow functions not working in node --harmony under Ubuntu

落花浮王杯 提交于 2019-11-30 17:50:02
I'm trying to use arrow functions in node v0.10.33 under Ubuntu 14.04 (I'm running node with --harmony flag), but I'm getting this error: console.log( [1,2,3,4].map(x => x*x) ); ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 This should work now in node v0.12.x, with the --harmony flag. Also note that you can get arrow functions in node using the babel

Converting Singleton JS objects to use ES6 classes

半世苍凉 提交于 2019-11-30 10:25:49
问题 I'm using ES6 with the Webpack es6-transpiler per my article here: http://www.railsonmaui.com/blog/2014/10/02/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/ Does it make any sense to convert two Singleton objects to use ES6 Classes? import { CHANGE_EVENT } from "../constants/Constants"; var EventEmitter = require('events').EventEmitter; var merge = require('react/lib/merge'); var _flash = null; var BaseStore = merge(EventEmitter.prototype, { emitChange: function()

Find all classes in a Javascript application that extend a base class

廉价感情. 提交于 2019-11-30 09:00:44
问题 I have code like this class Animal{} class Dog extends Animal {} class Cat extends Animal {} class Donkey extends Animal {} I want to look at all of the classes in my application's universe, and when I find one that descends from Animal, I want to create a new object of that type and add it to the list. This allows me to add functionality without having to update a list of things. So I can avoid the following: var animals = []; animals.push( new Dog() ); animals.push( new Cat() ); animals

Implementing monads in JavaScript

谁说胖子不能爱 提交于 2019-11-30 06:57:04
问题 Now that node.js supports ECMAScript Harmony generators we can write monadic code succinctly ala do blocks in Haskell: function monad(unit, bind) { return function (f) { return function () { var g = f.apply(this, arguments); return typeOf(g) === "Generator" ? send() : unit(g); function send(value) { var result = g.next(value); if (result.done) return unit(result.value); else return bind(result.value, send); } }; }; } function typeOf(value) { return Object.prototype.toString.call(value).slice

ES6 Modules vs. HTML Imports

久未见 提交于 2019-11-30 06:56:38
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 want to include the parent script too (avoiding manual dependency handling). There seems to be no well

Why can functions be called without parentheses when using template strings? [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-30 04:42:22
This question already has an answer here: Backticks calling a function 2 answers I have a simple logging function: function log(str) { console.log('logged: ', str); } If I call it without parentheses (currently using Chrome's dev tools) and pass in a template string, like this: log`foo` The output is: logged: ["foo", raw: Array[1]] If I call it with parentheses, log(`foo`) The output is: logged: foo Why does calling a function using a template string an no parentheses work in Javascript? What is happening that causes the result to be different from calling it with parentheses? Sampson The

Arrow functions not working in node --harmony under Ubuntu

送分小仙女□ 提交于 2019-11-30 02:46:00
问题 I'm trying to use arrow functions in node v0.10.33 under Ubuntu 14.04 (I'm running node with --harmony flag), but I'm getting this error: console.log( [1,2,3,4].map(x => x*x) ); ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3 回答1: This should work now

Converting Singleton JS objects to use ES6 classes

醉酒当歌 提交于 2019-11-29 19:54:31
I'm using ES6 with the Webpack es6-transpiler per my article here: http://www.railsonmaui.com/blog/2014/10/02/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/ Does it make any sense to convert two Singleton objects to use ES6 Classes? import { CHANGE_EVENT } from "../constants/Constants"; var EventEmitter = require('events').EventEmitter; var merge = require('react/lib/merge'); var _flash = null; var BaseStore = merge(EventEmitter.prototype, { emitChange: function() { this.emit(CHANGE_EVENT); }, /** * @param {function} callback */ addChangeListener: function(callback)