Tag: ecmascript 6

ES6和CommonJS出口惯例的缺点

对于由package.json定义的npm模块的main文件,我正在考虑一个这样的模式: import Struct from './src/struct' module.exports = Struct module.exports.default = Struct 为了支持CommonJS和ES6导入: const Struct = require('my-module') // or import Struct from 'my-module' 这个公约有什么缺点可能导致意想不到的问题吗? 我试图解决的问题是不得不迫使这个软件包的用户坚持ES6或CommonJS,因为这两者看起来相当令人讨厌: const Struct = require('my-module').default // or import * as Struct from 'my-module' 进一步考虑这个之后,会在/src/struct.js定义我的类是否更好? export default class Struct { static get default () { return Struct } … }

ES6使用构造函数参数设置这个属性

我不知道是否有简单的方法来设置这个属性与构造函数参数,如使用扩展运算符或其他方式? 这是设置道具的方式: constructor(id=0, name='', surname='') { this.Id = id; this.Name = name; this.Surname = surname; } 我正在寻找另一种方式来设置这个道具在一行:)

Babel 6 – 启用function的默认参数

我有一段代码: 'use strict'; class ArticleModel { constructor(options = {}) { this.options = options } } module.exports = ArticleModel 这会导致错误Unexpected token = – 我不相信Babelparsing这个。 需要哪个babel 6插件来parsing函数中的默认参数? 编辑1 – 这是我的.babelrc文件 { "presets": [ "es2015", "stage-0" ] } 编辑2 – 我没有运行与.babelrc相同的目录下的babel。 我从内部test/运行babel test/结构如下所示: /app /test /test/runner.js < — this is what calls babel-core/register .babelrc 我是否需要明确告诉babel-core/register .babelrc是哪里? 我认为它卷起了一个目录。 编辑3 […]

为什么我的class级原型不工作?

为了构build我的JavaScript代码,我想使用类 ,ES6的原型inheritance的语法糖。 虽然我遇到了麻烦:看来我的原型方法没有按预期工作,这让我觉得我可能对JavaScript类有一个基本的误解,和/或它们是如何工作的。 采取下面的代码,它声明了一个传统的构造函数Cat并修改了它的原型,还有一个ES6类的Dog ,里面定义了一个原型方法。 // Traditional Cat Constructor // =========================== function Cat(name) { this.name = name; this.sound = "Meow."; } Cat.prototype.speak = function() { console.log(this.sound || "I don't make a sound."); }; // Dog via ES6 Class // ================= class Dog { constructor(name) { this.name = name; this.sound = "Woof!"; } speak() { console.log(this.sound); […]

如何用Ecmascript 6类扩展对象文字类

我有一个基class被定义为一个object literal ,在构造函数的作用域内定义的方法。 喜欢这个: var NodeMappingAbstract = function NodeMappingAbstract() { this.mapToContent = function (obj) { throw new Error('Not implemented'); }; this.getInfo = function (data) { throw new Error('Not implemented'); }; this.normalizeUri = function normalizeUri(uri) { return uriNormalizer.normalize(uri); }; }; 如何用ES6 class语法扩展NodeMappingAbstract ? 我尝试使用extends和super() : class SomeMapping extends NodeMappingAbstract{ constructor(){ super(); } mapToContent(rawElement) { console.warn('mapContent called'); […]

使用Node或ES6从JSON过滤logging

我不确定最好的方式去做这件事。 我想迭代我的json并find所有在美国的公司。 这个JSON可能会随着我的应用程序的增长而变得更加复杂,就像在关卡,对象等等一样。我只是想知道人们在简单地search用JSON和Node.js和/或ES6或库过滤掉数据子集的方法也许如Lodash等 比如说这个JSON,我可以通过什么方式search它,并且只撤回在美国的那些公司呢? [{ "id": 0, "name": "Company1", "logoUrl": "/lib/assets/company1-logo.png", "location":{ "country": "USA", "state": "California", "city": "Napa" }, "active": false }, { "id": 1, "name": "Company2", "logoUrl": "/lib/assets/company2-logo.png", "location":{ "country": "Germany", "state": "", "city": "Berlin" }, "active": false }, { "id": 2, "name": "Company3", "logoUrl": "/lib/assets/company3-logo.png", "location":{ "country": "USA", "state": "Michigan", "city": "Detroit" }, […]

筛选被拒绝的承诺

我有一个Promise对象的数组,我想映射和筛选被拒绝的Promise 。 预期产出: const promises = [ failedPromise, successPromise, successPromise, ]; const resolvedPromises = promises.map(promise => ???); resolvedPromisesvariables应该容纳两个successPromise promise的输出。 我将如何实现这样的algorithm?

Node v6需要Babel transpiling吗?

考虑到Node v6.x具有〜93%的ES2015规范覆盖率,是否需要Babel transpiling? 我正在使用Node v6和Express创build一个Web应用程序。 如果这只是一个Node应用程序,我不会想到Babel,但考虑到也会有一些客户端JS,我想我应该有静态JS文件转发。 这是一个正确的假设吗? 如果需要,例如.babelrc会是什么? 目前,我在想: { "presets": [ "node6", "es2015" ] }

ES6从文件内容中进行string插值

我遇到了ES6中的string插值function。 它工作,如果我在代码中定义的原始string。 但我想从文件中读取原始string,然后replace占位符。 怎么做? file.txt的 hello ${customer.name} 节点JS var customer = {name: 'sid'}; var data = fs.readFileSync("file.txt","utf8"); // what should go here so that data is 'Hello sid'?

Node.js – 超出最大调用堆栈大小,即使使用process.nextTick()

我试图写一个“可链式”Express.jsvalidation模块: const validatePost = (req, res, next) => { validator.validate(req.body) .expect('name.first') .present('The parameter is required') .string('The parameter must be a string') .go(next); }; router.post('/', validatePost, (req, res, next) => { return res.send('Validated!'); }); validator.validate的代码(简洁起见): const validate = (data) => { let validation; const expect = (key) => { validation.key = key; // Here I get […]