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

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

问题描述



我正在尝试编写一个CSV文件并根据标题行动态生成定义的工具。例如,CSV: >

 标题(STRING),说明(TEXT)
标题示例,说明示例
...

Sequelize docs 指定,例如:

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

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






编辑



好的,经过一些研究,我认为明显的问题是如何使用变量名称作为动态



因此,以括号表示法写入这一点很简单:

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

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

然而,我的问题现在是如何使用ES6 节点中的计算属性名称?我正在使用我认为有ES6支持的节点0.12.2,甚至使用 - 和谐标志,这个简单的代码失败:

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

with SyntaxError:Unexpected token [



唯一的选择是真正的 io.js 吗? / p>

编辑2



实际上,即使使用iojs,这种语法仍然失败,所以我必须做错了什么?

解决方案

ECMAScript 6兼容性表显示了节点和io.js目前都不支持计算属性。数据位于对象文字扩展名> 计算属性


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

For example, a CSV with:

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

the Sequelize docs specify, for example:

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

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?


EDIT

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);

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 [

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

EDIT 2

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

解决方案

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.

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

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