如何在JavaScript对象中添加属性?

时间:2014-03-10 17:21:46

标签: javascript object

我有一个 JavaScript 对象:

var credentials = 
{ 
    uid: response.authResponse.userID, 
    accessToken: response.authResponse.accessToken 
};

如何添加此对象name: response.something

1 个答案:

答案 0 :(得分:3)

JavaScript对象是动态的。这意味着您可以随时添加任何属性,只需执行以下操作:

credentials.name = response.something;

或等效地:

credentials['name'] = response.something;

仅限ECMAScript 5:

Object.defineProperty( credentials, "name", {
    value: response.something,
    writable: true,
    enumerable: true,
    configurable: true
});