gpt4 book ai didi

javascript - JS字符串解构: rest parameter returning inconsistent data

转载 作者:行者123 更新时间:2023-12-03 23:01:38 28 4
gpt4 key购买 nike

考虑以下示例
一个老项目:

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/

28 4 0