Geters and setters


#1

Hi everyone!
I don’t understand one thing:

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('${name} is ${age} years old');
    return this._age;
  }
}
person.age = 'Thirty-nine';
person.age = 39;
console.log(person.age);

why ${name} and ${age} are inside a string?
the output is : ${name} is ${age} years old
instead of: Lu Xun is 39 years old
I still got it correct in the lesson but i don’t know why .


#2

Hi @kati210
There should be backticks (`) instead of ’ in the age getter
I don’t know know why you got it correct.
There must be a problem in codecademy
Moderators know this better


#3

es6 string interpolation (${}) which allows variables within a string, requires backticks, not apostrophes like you used

Sometimes, corner case slip through the cracks.


#4

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