2017-07-04 65 views
0

说我有一个对象,看起来像对象键此使用保留关键字与传播运营商

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

现在说我用传播经营者和做这样的事情

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

那么对于otherStuffs,我仍然收到一个props的副本,但hello键除外。

但是如果我不想要该对象的delete键?我不能这样做,因为显然delete是保留关键字。

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

我仍然不过从过滤的对象不等于“删除”,并让我的对象,但有没有办法做到这一点使用扩展运营商的钥匙?

+0

对象传播语法不ES6。 – Bergi

回答

3

您可以通过别名delete属性props来完成。你可以试试这个

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

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

参考:Invalid JavaScript identifier as a property name

+0

也可以使用符号吗? – evolutionxbox

+0

用符号表示新的ES6符号?你想在哪里使用它? –

+0

作为一个关键。就像你会一个字符串。 – evolutionxbox