+ 1

Why am I getting rang error ?

let person={fname:'john',age:20,get age(){return this.age}}; console.log(person.age)

6th Jan 2023, 1:57 PM
Levi
Levi - avatar
4 Answers
+ 1
Levi change the getter function name to getAge, key age and getter function are duplicate.
6th Jan 2023, 2:18 PM
Sudarshan Rai
Sudarshan Rai - avatar
0
Do you need make ?
6th Jan 2023, 2:19 PM
Henry
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)
6th Jan 2023, 2:28 PM
Emms
Emms - avatar
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.
7th Jan 2023, 6:00 AM
Emerson Prado
Emerson Prado - avatar