I was testing a getter in my class and I faced a contrast in my implementation and document, based on MDN document a class getter property should be defined inside the instance's prototype
Getter properties are defined on the prototype property of the class and are thus shared by all instances of the class.
so why I can see the getter directly on my instance,
the issue I have with this implementation is, if we assume the getter property is only inside the instance's prototype so why the this
is referred to the k
not the Kevin.prototype
because this
in a getter function inherites the object which is located in it based on the MDN document
A function used as getter or setter has its this bound to the object from which the property is being set or gotten.
so in a nutshell, why we have getc
directly in our class instance and why the getc
value inside the prototype of k
is lambo
not undefined
?
code:
class Kevin {
constructor(x) {
this.age = x;
this.car = "lambo"
}
get getc() {
return this.car;
}
}
const k = new Kevin(3);