评估 ES6 数组理解字符串
Evaluate ES6 Array Comprehension String
在 ES6 中,我可以使用
[for(i1 of [0, 1]) for(i2 of [0, 1]) [i1, i2]]
并得到
[[0, 0], [1, 0], [0, 1], [1, 1]]
我可以构建一个字符串,例如
str = '[for(i0 of [0, 1]) for(i1 of [0, 1]) [i0, i1]]'
或
str = '[for(i1 of [0, 1]) for(i2 of [0, 1]) for(i3 of [0, 1]) [i1, i2, i3]]'.
如何评估/执行这样的字符串?
你可以做与在 ECMAScript 5 中相同的操作。使用任一eval
:
var arr = eval(str);
或new Function
:
var arr = new Function('return' + str)();
使用 System.module
或 System.define
。
从 http://www.2ality.com/2014/09/es6-modules-final.html:
-
System.module(source, options?)
评估源代码中的 JavaScript 代码到模块(通过 promise 异步交付)。 -
System.set(name, module)
用于注册模块(例如,通过System.module()创建的模块)。 -
System.define(name, source, options?)
既计算源代码中的模块代码,又注册结果。
相关文章:
- 将JSON转换为typescript中的字符串数组
- 在Javascript中将一个值和字符串数组转换为if语句
- 如何将正则表达式包含到字符串数组中
- JavaScript:将字符串数组转换为文本区域
- Knockout中的字符串数组
- 拆分字符串数组(JavaScript)后未定义
- 使用Javascript获取两个字符串之间的字符串数组
- 打印从Spring MVC Controller发送的字符串数组只显示“;toJSON”;在javascript中
- 将一个字符串数组解析为一个新的数组javascript
- AngularJS - 从字符串数组中连续更改文本
- 使用字符串数组中的字符填充 HTML 表
- 如何在 Javascript 中将字符串数组列表插入到表行中
- 创建二维字符串数组
- 如何从字符串数组中获取子字符串数组
- 如何将字符串数组转换为简单数组
- 按字母顺序、数字顺序和特殊字符对字符串数组进行排序
- 如何从字符串数组中获得8个随机唯一元素
- json_encode($myVar);正在给出地图,我想要一个字符串数组
- 函数,返回javascript中两个字符串数组的差值
- Javascript字符串数组转换为用逗号分隔的多个字符串