Reputation: 25
class Class3 {
get service() {
return 'service'
}
}
const class3Instance = new Class3()
console.log(class3Instance.service)
console.log(class3Instance)
// 👆 clas3Instance had Semi transparent service
var descriptor = Object.getOwnPropertyDescriptor(class3Instance, 'service')
console.log(descriptor, "descriptor")
// 👆 undefined
I expected class3Instance
has service property but it didn't.
Upvotes: 1
Views: 46
Reputation: 371049
Just like any methods defined directly inside a class
, it's a property on the prototype, not on the object itself. Check the descriptor of Class3.prototype
, or of Object.getPrototypeOf(class3Instance)
:
class Class3 {
get service() {
return 'service'
}
}
const class3Instance = new Class3()
var descriptor = Object.getOwnPropertyDescriptor(Class3.prototype,'service')
console.log(descriptor,"descriptor")
The class3Instance
object does not have an own property of service
- when class3Instance.service
is accessed, the interpreter finds that property name on the internal prototype of class3Instance
, not on class3Instance
itself.
Upvotes: 2