-1

I have a JavaScript object:

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

How can I add in this object name: response.something?

user2864740
  • 60,010
  • 15
  • 145
  • 220
user2406735
  • 247
  • 1
  • 6
  • 21

1 Answers1

3

JavaScript objects are dynamic. That means that you can add any property to an object at any time simply by doing this:

credentials.name = response.something;

or equivalently:

credentials['name'] = response.something;

ECMAScript 5 only:

Object.defineProperty( credentials, "name", {
    value: response.something,
    writable: true,
    enumerable: true,
    configurable: true
});
Bradley Trager
  • 3,570
  • 3
  • 26
  • 33