I can't figure it out. I need to use a special method for my purpose - Object.getOwnPropertyDescriptor()
.
This is my code:
function Album(title, artist, genre, year) {
this.title = prompt();
this.artist = prompt();
this.genre = prompt();
this.year = prompt();
}
let album1 = new Album()
for (let key in album1) {
if (album1.hasOwnProperty(key)) {
console.log("Property: " + key);
console.log(Object.getOwnPropertyDescriptor(album1, key))
console.log("...")
}
}
I need to get the data like this:
Property: title
value: Thriller
configurable: true
enumerable: true
writable: true
...
Property: artist
value: Michael Jackson
configurable: true
enumerable: true
writable: true
...
Property: genre
value: Pop
configurable: true
enumerable: true
writable: true
...
Property: year
value: 1982
configurable: true
enumerable: true
writable: true
...
Now I'm getting a result that I want. I've taken a screenshot of what I've managed to do so far.
This is result of my code for now:
Property: title
undefined
...
Property: artist
undefined
...
Property: genre
undefined
...
Property: year
undefined
...
I can't figure it out. I need to use a special method for my purpose - Object.getOwnPropertyDescriptor()
.
This is my code:
function Album(title, artist, genre, year) {
this.title = prompt();
this.artist = prompt();
this.genre = prompt();
this.year = prompt();
}
let album1 = new Album()
for (let key in album1) {
if (album1.hasOwnProperty(key)) {
console.log("Property: " + key);
console.log(Object.getOwnPropertyDescriptor(album1, key))
console.log("...")
}
}
I need to get the data like this:
Property: title
value: Thriller
configurable: true
enumerable: true
writable: true
...
Property: artist
value: Michael Jackson
configurable: true
enumerable: true
writable: true
...
Property: genre
value: Pop
configurable: true
enumerable: true
writable: true
...
Property: year
value: 1982
configurable: true
enumerable: true
writable: true
...
Now I'm getting a result that I want. I've taken a screenshot of what I've managed to do so far.
This is result of my code for now:
Property: title
undefined
...
Property: artist
undefined
...
Property: genre
undefined
...
Property: year
undefined
...
Share
Improve this question
edited 4 hours ago
Quill
2,7551 gold badge35 silver badges45 bronze badges
asked Feb 7 at 20:04
DerekDerek
553 bronze badges
New contributor
Derek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6
|
Show 1 more comment
1 Answer
Reset to default 2You're calling the wrong function. You're calling the function that returns all property descriptors, not the function for a single property. And the second argument should be the property key, not the property value.
console.log(Object.getOwnPropertyDescriptors(album1, album1[key]))
should be
console.log(Object.getOwnPropertyDescriptor(album1, key))
Object.getOwnPropertyDescriptors
does not make a lot of sense because its result is known in advance. And if it is done with experimental purpose, only one property would suffice, just because other properties are created in exact same way. But... not happy... why? What are you supposed to be happy with? – Sergey A Kryukov Commented Feb 7 at 20:26Object.getOwnPropertyDescriptors()
only takes 1 argument. It returns the descriptors of all properties. Did you mean to useObject.getOwnPropertyDescriptor()
, which takes an object and property name, and just returns that one descriptor? – Barmar Commented Feb 7 at 20:31