javascript - JS字符串解构:rest参数返回不一致的数据
问题描述
考虑以下示例
一个老项目:
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 网站中,您可以看到您的基于 es2015-loose 的代码转换为此代码,因此此代码的输出与您的旧项目相同
"use strict";
var _text = "text",
x = _text[0],
y = _text.slice(1);
console.log(x); // "t"
console.log(y); // "ext"
推荐阅读
- c# - 如何使用按钮单击事件创建对象并将它们添加到列表中?
- python - PyQt5 QListView 已停止从 QSqlTableModel 加载条目,我不知道为什么
- bash - 如何使用 bash 命令更改 XAMPP DocumentRoot?
- ios - iOS 13 快捷方式:地图操作搜索本地企业的问题
- linux - 如何在文件中显示 bash shell 的所有命令?Linux
- django - 在 Django 模板中显示银行账户余额的推荐方法
- symfony - Symfony 多域登录
- discord.js - 如何获取我的 Discord 机器人的创建日期?
- c - 访问返回的指针时出现分段错误
- html - 将 url 插入自定义 Django 电子邮件模板