使用保留关键字作为带有扩展运算符的对象键
Using reserved keywords as object key with spread operators
假设我有一个看起来像这样的对象
const props = {
delete: function() {
// stuffs
},
hello: 'hello',
other: 'other',
}
现在假设我使用传播运算符并做这样的事情
const {hello, ...otherStuffs} = props;
然后对于 otherStuffs,我仍然得到一个对象,该对象是 props
的副本,但 hello
键除外。
但是如果我不想要对象的 delete
键怎么办?我不能像上面那样做,因为显然 delete
是保留关键字。
const {delete, ...otherStuffs} = props; // error here
不过,我仍然可以从对象中过滤不等于 'delete' 的键并获取我的对象,但是有没有办法 使用展开运算符 ?
您可以通过为 props
的 delete
属性 添加别名来实现。你可以试试这个
const props = {
delete: function() {
console.log("delete");
},
hello: 'hello',
other: 'other',
}
const {"delete": del, ...otherStuffs} = props;
del();
假设我有一个看起来像这样的对象
const props = {
delete: function() {
// stuffs
},
hello: 'hello',
other: 'other',
}
现在假设我使用传播运算符并做这样的事情
const {hello, ...otherStuffs} = props;
然后对于 otherStuffs,我仍然得到一个对象,该对象是 props
的副本,但 hello
键除外。
但是如果我不想要对象的 delete
键怎么办?我不能像上面那样做,因为显然 delete
是保留关键字。
const {delete, ...otherStuffs} = props; // error here
不过,我仍然可以从对象中过滤不等于 'delete' 的键并获取我的对象,但是有没有办法 使用展开运算符 ?
您可以通过为 props
的 delete
属性 添加别名来实现。你可以试试这个
const props = {
delete: function() {
console.log("delete");
},
hello: 'hello',
other: 'other',
}
const {"delete": del, ...otherStuffs} = props;
del();