当我创建javascript对象的动态键时,它会破坏gulp缩小过程。
var a = "custome_name"
var b = {[a]:"myName"}
// {custome_name: "myName"} - works fine but breaks in gulp minification task.
我可以使用哪种其他语法?
答案 0 :(得分:1)
The problem is that you are using a variable (a
) as a property name inside an object literal.
While this is valid for ES6, it is not for ES5 syntax. The Gulp minifier you are using (presumably Uglify2) does not support minification of ES6 syntax.
If you don't want to use a transpiler like Babel to convert your code from ES6 to ES5, you can rewrite your code to work around the issue like this:
var a = "custome_name"
var b = {}
b[a] = "myName"
Instead of creating the object with the dynamic key directly, create an empty object first, which you can then assign the value to using the dynamic key and brackets.
→ See related question: Using a Variable for a Key in a JavaScript Object Literal