Getter and setter ,what wrong is it?


#1
 let person = {
    _name: 'Lu Xun',
    _age: 137,
    set age(ageIn){
    if(typeof ageIn==='number'){
      this._age=ageIn;
    }else{
      return 'Invalid input'}
     }
    get age(){
    console.log(`${this._name}is ${this._age}  years old`);
    return this._age;
   }
    };
     person.age='Thirty-nine';
     person.age=39;

debugger told me:

get age(){
^^^
Syntax Error: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._ext.....

 my question: in fact ,it was not the first time which told me ,when i wrote set (){} 
    and the get age(){} they were  wrong ,but i tried several times to correct it .but failed .why 
  ?
 thank you for your help.

#2

here:

set age(ageIn){
    if(typeof ageIn==='number'){
      this._age=ageIn;
    }else{
      return 'Invalid input'}
     }

you forgot }, the } to close the setter method, and the comma (,) to separate the properties


#3

thank you for your help


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.