Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While using the Object.defineProperties in JavaScript I am getting as ' undefined' on newly defined property .How to fix it..?

const object1 = {
    firstName : 'Shashidhar',
    lastName : 'B M ',
    rollNo : 5678,
    rank : 23456

}

Object.defineProperties(object1,{
    property1 : {
    results : 'selected'
    }
});


console.log(object1.property1)
console.log(object1.firstName);

expected output

selected
shashidhar

actual output

undefined
shashidhar

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You want value rather than results to specify the property’s value:

const object1 = {
    firstName : 'Shashidhar',
    lastName : 'B M ',
    rollNo : 5678,
    rank : 23456
}

Object.defineProperties(object1,{
    property1 : {
    value : 'selected'
    }
});


console.log(object1.property1)
console.log(object1.firstName);

See the documentation on MDN.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading