考虑以下示例
一个老项目:
const [x, ...y] = "text";
console.log(x) // "t"
console.log(y) // "ext"
基于 CRA 的新项目:const [x, ...y] = "text";
console.log(x) // "t"
console.log(y) // ["e", "x", "t"]
我不知道为什么y
正在为旧项目返回一个字符串( "ext"
),其中它是新项目的字符数组( ["e", "x", "t"]
)。是否与不同的JS版本有关?注意:两个结果都是在运行 webpack 开发服务器后提取的。
最佳答案
在 babel website您可以看到您的基于 es2015-loose 的代码转换为此代码,因此此代码的输出与您的旧项目相同
"use strict";
var _text = "text",
x = _text[0],
y = _text.slice(1);
console.log(x); // "t"
console.log(y); // "ext"
关于javascript - JS字符串解构: rest parameter returning inconsistent data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65367838/