Tag: ecmascript 6

在JavaScript中可以inheritanceECMAScript 6类中的旧式类吗?

在Node.js 4.2.1上运行以下代码时: 'use strict'; var util = require('util'); class MyClass { constructor(name) { this.name = name; } } function MyDerived() { MyClass.call(this, 'MyDerived'); } util.inherits(MyDerived, MyClass); var d = new MyDerived(); 我得到以下错误: constructor(name) { ^ TypeError: Class constructors cannot be invoked without 'new' 我想知道是否可以inheritanceECMAScript 6类的旧式JavaScript“类”? 而且,如果可能的话,那么怎么样?

对象字面量(散列)与Promise.all

我有一种使用Promise.all就像Promise.all({})而不是更标准的Promise.all([])。 但是这似乎不起作用 Promise.all({a:1,b:2}).then(function(val){ console.log('val:',val); }); 虽然这当然 Promise.all([1,2,3]).then(function(val){ console.log('val:',val); }); (我期望的是为Promise.all映射对象文字的值,但保持键不变。) 但MDN文件的承诺似乎表明,承诺所有将工作任何迭代。 就我所知,一个对象字面{}是一个可迭代的。 那么我错过了什么?

使用伊斯坦布尔和摩卡来覆盖ES6的代码

我有用ES6编写的Node代码,通过发布mocha –harmonytesting。 testing很好 – 一切正常。 现在我想添加覆盖和伊斯坦布尔的混合,但我不断遇到遇到的第一个箭头函数的错误: No coverage information was collected, exit without writing coverage information c:\Users\Guy\Code\alpha-dev\tests\helpers.js:12 setTimeout(() => { ^ SyntaxError: Unexpected token ) at exports.runInThisContext (vm.js:73:16) at Module._compile (module.js:443:25) at Module._extensions..js (module.js:478:10) at Object.Module._extensions..js (c:\Users\Guy\Code\alpha-dev\node_modules\istanbul\lib\hook.js:101:13) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) 这是我的尝试: 安装伊斯坦布尔和谐(从git://github.com/gotwarlost/istanbul.git#harmony)作为我的开发依赖。 运行以下命令: "./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" — –harmony tests […]

我可以导入babel-polyfill模块而不是全部?

如何导入babel-polyfill的某些特定模块而不是全部导入? 对于我来说,它的大小似乎太大了,我只用了它的一些特性。 我想要的是如下: import "babel-polyfill/symbol";

使用Jesttesting失败,错误:观察文件更改时出错:EMFILE

我试图编写一个React.js应用程序的testing。 一切都很顺利,但使目录跟踪Git(做了一个Git回购)。 testing失败,下面的错误 2017-01-15 05:05 node[1278] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) 2017-01-15 05:05 node[1278] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) events.js:160 throw er; // Unhandled 'error' event ^ Error: Error watching file for changes: EMFILE at exports._errnoException (util.js:1022:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1406:11) 我相信这是因为.git目录,因为当我删除.git目录时,它正在运行没有错误。 似乎在观看文件时发生exception。 我的开发环境是MacOS 10.12.2和Node 6.9.4。 我该如何解决这个问题?

与本地承诺循环;

我试图做一个asynchronous循环与本地ES6承诺 这种作品,但不正确的。 我想我在某个地方犯了一个大错,我需要有人告诉我它在哪里以及它是如何正确完成的 var i = 0; //creates sample resolver function payloadGenerator(){ return function(resolve) { setTimeout(function(){ i++; resolve(); }, 300) } } // creates resolver that fulfills the promise if condition is false, otherwise rejects the promise. // Used only for routing purpose function controller(condition){ return function(resolve, reject) { console.log('i =', i); condition ? reject('fin') […]

如何在ES6中获得对类函数的引用?

对不起,如果问题太简单了,但我在这里错过了一些东西。 刚刚切换了一个如下所示的ES5模块: module.exports = { func1: function(a, b) {…}, func2: function(a, b) {…} }; 对于看起来像这样的ES6类: export default class { func1(a, b) {…} func2(a, b) {…} } 一切都很好:在这两种情况下,我都可以export mod from 'module'; 并调用mod.func1(a, b)和mod.func2(a, b) 。 但是,我有一个function,接收模块函数来调用: var caller = function(func, val1, val2) { let a = something(val1); let b = something(val2); return func(a, b); }; 当我调用调用caller(mod.func1, […]

添加到`WeakSet`,但仍然打击记忆

这是我注意到的奇怪。 下面的代码不应该像WeakSet使用内存,显然没有其他引用逗留在周围: 'use strict'; require('babel-polyfill'); const s = new WeakSet(); for (let i = 0 ; ; i++) { s.add({}); if (i % 100000 === 0) console.log(`${i} :${process.memoryUsage().heapUsed}`); } (SCCE github回购这里 )。 然后打开它的记忆(在Node v4.3.2中用Babel transpiling): <— Last few GCs —> 165 ms: Scavenge 13.6 (48.0) -> 13.6 (48.0) MB, 14.4 / 0 ms [allocation failure]. […]

实施Promise.series作为Promise.all的替代品

我看到Promise.all的这个示例实现 – 它并行运行所有promise – 实现Promise.all 请注意,我正在寻找的function类似于Bluebird的Promise.mapSeries http://bluebirdjs.com/docs/api/mapseries.html 我正在尝试创buildPromise.series,我有这个似乎按预期工作( 它实际上是完全错误的,不使用它,看答案 ): Promise.series = function series(promises){ return new Promise(function(resolve,reject){ const ret = Promise.resolve(null); const results = []; promises.forEach(function(p,i){ ret.then(function(){ return p.then(function(val){ results[i] = val; }); }); }); ret.then(function(){ resolve(results); }, function(e){ reject(e); }); }); } Promise.series([ new Promise(function(resolve){ resolve('a'); }), new Promise(function(resolve){ resolve('b'); }) ]).then(function(val){ console.log(val); }).catch(function(e){ […]

如何添加mixin ES6的JavaScript类?

在具有一些实例variables和方法的ES6类中,如何添加一个mixin? 我在下面给出了一个例子,虽然我不知道mixin对象的语法是否正确。 class Test { constructor() { this.var1 = 'var1' } method1() { console.log(this.var1) } test() { this.method2() } } var mixin = { var2: 'var2', method2: { console.log(this.var2) } } 如果我运行(new Test()).test() ,它会失败,因为类中没有method2 ,因为它在mixin中,所以我需要将mixinvariables和方法添加到类中。 我看到有一个lodash mixin函数https://lodash.com/docs/4.17.4#mixin ,但我不知道如何使用它与ES6类。 我很好地使用lodash的解决scheme,甚至没有库提供mixinfunction的纯JS。