2017-07-04 38 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