Tag: ecmascript 6

我如何在Node 5.6.0环境中运行Node 6 npm包?

我目前正在使用一个运行跨浏览器JavaScripttesting的软件包,称为easy-sauce 。 简而言之,我的package.json文件调用test命令: { "scripts": { "test": "easy-sauce" } } 当我使用Node 6环境,并运行npm test ,一切正常。 但是,我使用的项目仍然需要Node 5.6.0。 当我在这个环境中运行npm test时,我看到以下错误: /data/projects/easytest/node_modules/easy-sauce/lib/cli.js:114 function formatResult(result = {}) { ^ SyntaxError: Unexpected token = at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at Object.<anonymous> (/data/projects/easytest/node_modules/easy-sauce/bin/easy-sauce:6:13) at Module._compile […]

ExpressJS中的ES6类有不同的performance?

为什么一个类在ExpressJS中有不同的performance? 举个例子: 随着expressJS LIB / Polygon.js: class Polygon { log (req, res) { var publicKey = req.params.publicKey; var query = req.query; console.log(this); // undefined var output = { publicKey :publicKey, query: query }; res.send(output); } } export {Polygon as default} app.js: import express from 'express'; import Polygon from './lib/Polygon'; var polygon = new Polygon(); app.get('/input/:publicKey', […]

是否有可能在Node REPL中运行ES6?

有没有办法在节点REPL中运行ES6(Read Evaluate Print Loop)? 运行ES 6命令时,出现如屏幕截图所示的错误。 欣赏是否有人可以帮助我configurationNode来运行ES6代码。

node.js简单的项目:ReferenceError:<ClassName>没有定义

我试图学习Node.js(ES6),但在require失败 这是我的结构: baseModel.js "use strict"; class BaseModel { constructor(options = {}, data = []) { // class constructor this.name = 'Base' this.url = 'http://azat.co/api' this.data = data this.options = options } getName() { // class method console.log(`Class name: ${this.name}`) } } AccountModel.js "use strict"; require('./baseModel.js'); class AccountModel extends BaseModel { constructor(options, data) { super({private: true}, […]

使导出默认使用Babel,webpack和Node.js

我无法弄清楚为什么我的代码不起作用。 我正在构build一个ES6风格的类,并希望导出它能够在服务器端的其他地方使用它。 我把代码放到一个名为PlayerManager.js的server文件夹中。 我把我的客户端代码放在src文件夹中。 而我的服务器代码在我的server文件夹和server文件夹外的server.js 。 这是目录结构: Root – dist – node_modules – public – server – src server.js webpack.base.js webpack.dist.js package.json .babelrc PlayerManager.js文件: class PlayerManager { constructor() { if (! PlayerManager.instance) { this.playerList = {}; this.bulletList = {}; this.initPack = {player: [], bullet: []}; this.removePack = {player: [], bullet: []}; PlayerManager.instance = this; } […]

单例inheritanceBuggy行为

我已经发现在使用Singleton模式的JavaScript es6inheritance中的马车行为。 代码是 : let instanceOne = null; class One { constructor() { if (instanceOne) return instanceOne; this.name = 'one'; instanceOne = this; return instanceOne; } method() { console.log('Method in one'); } } let instanceTwo = null; class Two extends One { constructor() { super(); if (instanceTwo) return instanceTwo; this.name = 'two'; instanceTwo = this; […]

将cookie的值设置为模板文字?

我正在创build一个模板文字,如下所示: const someVar = 'hello' const str = ` Some random multiline string with string interpolation: ${someVar} ` 然后在我的Koa应用程序中,我正在做: this.cookies.set('str', str) 显然它不喜欢多行string,因为它给出了这个错误: TypeError:参数值无效 有没有办法解决? 保持空白格式对我来说是非常必要的。

我必须在Node.js中使用Babel吗?

我知道Node.js现在完全支持ES6(使用nodejs 7.2.1)。 有人告诉我,Node.js的ES6实现不是生产准备好的,我必须有Babel才能生产ES6。 我读了babeljs.io,它是一个不支持ES6的老浏览器的JavaScript编译器。 我有点困惑。 Node.js是否需要Babel编译成ES5? 或者我可以使用ES6与Node.js? 生产准备好了吗? 我真的需要Babel和Node.js吗?

es6导出/导入多个类 – instanceof返回false

test1.js export class ValidationError extends Error { constructor (msg) { super(msg) } } export class ServerError extends Error { constructor (msg) { super(msg) } } test2.js import * as errors from './test1' const inst = new errors.ValidationError('msg') console.log(inst instanceof errors.ValidationError) 当我运行test2输出是false ( true预期)。

在es6 javascript类的非静态成员函数中调用静态getter

我如何从ES 6类中的普通成员函数调用静态函数? 这里是一个例子: class Animal { constructor(text) { this.speech = text; } static get name() { return "Animal"; } speak() { console.log( this.name + ":"+ this.speech) } } class Tiger extends Animal { static get name() { return "Tiger" } } var animal = new Animal("hey there"); animal.speak(); var tiger = new Tiger("hello"); tiger.speak(); // […]