gpt4 book ai didi

javascript - 使用保留关键字作为带有扩展运算符的对象键

转载 作者:行者123 更新时间:2023-11-28 17:53:49 26 4
gpt4 key购买 nike

假设我有一个看起来像这样的对象

const props = {
delete: function() {
// stuffs
},
hello: 'hello',
other: 'other',
}

现在假设我使用扩展运算符并执行类似的操作

const {hello, ...otherStuffs} = props;

然后对于 otherStuffs,我仍然得到一个对象,它是 props 的副本,但 hello 键除外。

但是如果我不想要对象的 delete 键怎么办?我无法执行与上面相同的操作,因为显然 delete 是保留关键字。

const {delete, ...otherStuffs} = props; // error here

但是,我仍然可以从对象中过滤不等于“删除”的键并获取我的对象,但是有没有办法使用扩展运算符来做到这一点?

最佳答案

您可以通过为 propsdelete 属性添加别名来完成此操作。你可以试试这个

const props = {
delete: function() {
console.log("delete");
},
hello: 'hello',
other: 'other',
}

const {"delete": del, ...otherStuffs} = props;
del();

引用:Invalid JavaScript identifier as a property name

关于javascript - 使用保留关键字作为带有扩展运算符的对象键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com