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 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.
Share Improve this question asked Sep 6, 2018 at 9:51 Zuhair TahaZuhair Taha 3,0322 gold badges37 silver badges37 bronze badges1 Answer
Reset to default 19The 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.