+ 1
Why am I getting rang error ?
let person={fname:'john',age:20,get age(){return this.age}}; console.log(person.age)
4 Answers
+ 1
Levi change the getter function name to getAge,
key age and getter function are duplicate.
0
Do you need make ?
0
Levi
The reason your code is not returning 20 as expected but a RangeError is because the value of the property (age) is not 20 anymore but now a function. The value 20 got replaced by the getter function that keeps returning itself.
I made some adjustments to your code and now 20 is returned as expected.
let person={
fname:'john',
_age:20,
get age(){
return this._age}
};
console.log(person.age)
0
Emms Pls avoid giving finished code as answer, because it makes the OP to skip the most important part of learning. Always prefer giving hints for the OP to find the solution.