Nikita Shchypylov
Nikita Shchypylov

Reputation: 367

Why i am getting undefined?

Here is my code:

var person = {
    name: 'Greg',
    year: 20
};
Object.defineProperties(person, {
    gender :{
        value: 'male'
    },
    edition : {
        value : 12
    },
    edition : {
        set:function  (No) {
            if (No===13) {
                console.log('Yes')
            };
        }
    }

})
console.log(person.edition)

looks like i did all right, did not change descriptors any ideas? thanks

Upvotes: 1

Views: 44

Answers (1)

P. Jairaj
P. Jairaj

Reputation: 1033

When you write person.edition, you are calling the get method. Which is not defined. To call the set method you need to do assignment. Try this:

var person = {
    name: 'Greg',
    year: 20
};
Object.defineProperties(person, {
    "gender" :{
        value: 'male'
    },
    "edition" : {
        value : 12
    },
    "edition" : {
        set:function  (No) {
            if (No===13) {
                console.log('Yes')
            };
        }
        , get:function  () {
            return "hi";
        }
    }

})
console.log(person.edition)
person.edition = 13;

Upvotes: 1

Related Questions