3

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.

3
  • 4
    client.name = page; would probably be easier Commented May 23, 2018 at 16:38
  • 1
    Where is it returning undefined? Commented May 23, 2018 at 16:49
  • 1
    Maybe you are not passing a second argument to methodA? Commented May 23, 2018 at 17:03

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that and to check that it was working after I defined the property, and I try to client.name, value comes as undefined
I've edited the snippet for the console to also log specifically "client.name". It works. Does the snippet not work on your computer?
Thanks a lot on the response I had a typo on my code!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.