简体   繁体   English

评估ES6数组理解字符串

[英]Evaluate ES6 Array Comprehension String

In ES6, I can use 在ES6中,我可以使用

[for(i1 of [0, 1]) for(i2 of [0, 1]) [i1, i2]]

and get 并得到

[[0, 0], [1, 0], [0, 1], [1, 1]]



I can build a string like 我可以建立一个像

str = '[for(i0 of [0, 1]) for(i1 of [0, 1]) [i0, i1]]'

or 要么

str = '[for(i1 of [0, 1]) for(i2 of [0, 1]) for(i3 of [0, 1]) [i1, i2, i3]]'.

How to evaluate/excute such a string? 如何评估/执行这样的字符串?

You can do the same thing as you would do in ECMAScript 5. Using either eval : 您可以执行与ECMAScript 5中相同的操作。使用任一eval

var arr = eval(str);

or new Function : new Function

var arr = new Function('return' + str)();

Use System.module or System.define . 使用System.moduleSystem.define

From http://www.2ality.com/2014/09/es6-modules-final.html : http://www.2ality.com/2014/09/es6-modules-final.html

  • System.module(source, options?) evaluates the JavaScript code in source to a module (which is delivered asynchronously via a promise). System.module(source, options?)将源代码中的JavaScript代码评估到模块(通过promise异步交付)。

  • System.set(name, module) is for registering a module (eg one you have created via System.module()). System.set(name, module)用于注册一个模块(例如,您通过System.module()创建的模块)。

  • System.define(name, source, options?) both evaluates the module code in source and registers the result. System.define(name, source, options?)都评估source中的模块代码并注册结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM