如何在 node/iojs 中使用 ES6 计算属性名称? [英] How to use ES6 computed property names in node / iojs?

查看:31
本文介绍了如何在 node/iojs 中使用 ES6 计算属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个接受 CSV 并根据标题行动态生成定义的工具?

I am trying to write a tool that takes a CSV and dynamically generates a definition based on the header row?

例如,CSV 包含:

Title(STRING), Description(TEXT)
Title Example, Description Example
...

Sequelize docs 指定,例如:

the Sequelize docs specify, for example:

var Entry = sequelize.define('Entry', {
  title: Sequelize.STRING,
  description: Sequelize.TEXT
})

我如何编写这个定义以便它可以被动态定义 - 以便 title 和数据类型 Sequelize.STRING 可以基于 CSV 标头动态生成行?

How could I write this definition so that it could be dynamically defined - so that title and the data type Sequelize.STRING could be dynamically generated based on the CSV header row?

编辑

好的,经过一番研究,我认为显而易见的问题是如何在对象文字中使用变量名作为动态键名",并且已经回答了好几次.

Ok, after some research, I think the obvious question is "How to use variable names as dynamic key names in object literal" and has been answered several times.

因此,用括号表示法很简单:

As a result, it is simple to write this in bracket notation so:

var definitionObj = {}
definitionObj['title'] = sequelize.STRING;
definitionObj['description'] = sequelize.TEXT;

var Entry = sequelize.define('Entry', definitionObj);

但是,那么我现在的问题是 如何使用 ES6 节点中的计算属性名称?我正在使用我认为支持 ES6 的节点 0.12.2,即使使用 --harmony 标志,这个简单的代码也会失败:

However, then my question now is how do I use ES6 Computed Property Names in node? I'm using node 0.12.2 which I thought had ES6 support, and even with the --harmony flag, this simple code fails:

var Entry = sequelize.define('Entry', {
  ['title']: Sequelize.STRING,
  ['description']: Sequelize.TEXT
});

with SyntaxError: Unexpected token [

io.js 真的是唯一的选择吗?

Is the only option really to go to with io.js?

编辑 2

实际上,即使使用 iojs,这个语法仍然失败,所以我一定是做错了什么?

Actually this syntax still fails even with iojs, so I must be doing something wrong?

推荐答案

ECMAScript 6 兼容性表 表明 Node 和 io.js 目前都不支持计算属性.数据位于 对象字面量扩展 > 计算属性.

The ECMAScript 6 compatibility table shows that neither Node nor io.js currently have support for computed properties. The data is under object literal extensions > computed properties.

这篇关于如何在 node/iojs 中使用 ES6 计算属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆