elcharrua
elcharrua

Reputation: 1682

JS Adding property to object

I'm trying to add a property to an object the following way:

function methodA(client, page){
    Object.defineProperty(client, 'name', {
        value: page,
        writable: true,
        enumerable: true,
        configurable: true
      });

    methodB(client)
}

When I do a console log of client.name in methodB it returns undefined. Can someone point me out what I'm doing wrong ? I'm new to JS.

Upvotes: 3

Views: 119

Answers (1)

Michael Beeson
Michael Beeson

Reputation: 2875

Giving your code some dummy values it seems to work perfectly well. The error must be elsewhere. Run the snippet and see:

function methodA(client, page){
    Object.defineProperty(client, 'name', {
        value: page,
        writable: true,
        enumerable: true,
        configurable: true
      });

    methodB(client)
}

function methodB(client) {
    console.log(client);
    console.log("Name property is: "+client.name);
}

methodA({a:9}, 12);

Maybe try to make a snippet the gives the same error (you might stumble into the solution by yourself in doing so)

Upvotes: 2

Related Questions