I have javascript this class :
class Student {
constructor(name, birthDate) {
this.name = name;
this.birthDate = birthDate;
}
get age() {
return 2018 - this.birthDate;
}
display() {
console.log(`name ${this.name}, birth date: ${this.birthDate}`);
}
}
console.log(Object.getOwnPropertyNames.call(Student));
I want to get properties list names. I tried to use something like this:
Object.getOwnPropertyNames.call(Student)
But it doesn't work. what should I get in this example is name and birthDate only. without other methods or getters.
The issue is that you're using Object.getOwnPropertyNames
wrong. You don't need to use call
on it, you just call it.* And you need to pass an instance of Student
; the class object itself doesn't have any properties. The properties are created in the constructor, and nothing can tell you what properties the instance will have from looking at the class object.
class Student {
constructor(name, birthDate) {
this.name = name;
this.birthDate = birthDate;
}
get age() {
return 2018 - this.birthDate;
}
display() {
console.log(`name ${this.name}, birth date: ${this.birthDate}`);
}
}
console.log(Object.getOwnPropertyNames(new Student));
* If anything, Object.getOwnPropertyNames.call(Object, new Student)
does what you want, but that's nonsensical.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With