最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I can't get the property using Object.getOwnPropertyDescriptor method - Stack Overflow

programmeradmin0浏览0评论

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
  • Sorry, where is your question? You did not explain what you are not happy with. I would only note that the code using 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:26
  • I need to get the data like this: Property: title value: Thriller configurable: true enumerable: true writable: true . How can I do this? – Derek Commented Feb 7 at 20:29
  • 1 Object.getOwnPropertyDescriptors() only takes 1 argument. It returns the descriptors of all properties. Did you mean to use Object.getOwnPropertyDescriptor(), which takes an object and property name, and just returns that one descriptor? – Barmar Commented Feb 7 at 20:31
  • 1 Please post code, data, and results as text, not screenshots (how to format code in posts). Why should I not upload images of code/data/errors? idownvotedbecau.se/imageofcode – Barmar Commented Feb 7 at 20:39
  • 1 Please don't fix the code here as answers are posted. It makes things look really odd and hard to follow when people are suggesting code changes you've already made. :P If you really want to update the post to mention helpful changes you've implemented, it's better if that's clearly set off as an update. – cHao Commented Feb 7 at 20:58
 |  Show 1 more comment

1 Answer 1

Reset to default 2

You'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))
发布评论

评论列表(0)

  1. 暂无评论