-1
給定一個對象和一個鍵,「addProperty」在給定的對象上設置一個值爲true的新屬性。如何在給定對象上設置新屬性?
var myObj = {};
addProperty(myObj, 'myProperty');
console.log(myObj.myProperty); // --> true
這是我的代碼,但它是錯誤的:
function addProperty(obj, key) {
// your code here
obj.key = key;
obj.values = true;
return obj;
}
你是不是給你的對象的屬性'myProperty',您的加入屬性'key'和'value'。 –
'obj [key] = true;' – dfsq