-2
let person = {
  _name: 'Lu Xun',
  _age: 137,

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

};
console.log(person.set age('bdhh'));

// While executing the code is is giving error as uncaught reference error

void
  • 36,090
  • 8
  • 62
  • 107
Danny
  • 193
  • 12
  • When I run that I get `SyntaxError: missing ) after argument list` and not the error you say it gives. Try providing a real [mcve] (and quote the **full** error message)\ – Quentin Feb 27 '18 at 11:26
  • 1
    The age is a setter, hence *person.age='bdhh';* – Igor Dimchevski Feb 27 '18 at 11:27
  • `person.set age('bdhh')` that's not how setters are used - a quick trip to documentation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set#Examples – Jaromanda X Feb 27 '18 at 11:29

3 Answers3

1

You're invoking the setter the wrong way.

person.age = 15; // this is how you call your setter

See your updated code in jsFiddle: https://jsfiddle.net/t3uzpobn/4/

Alexander Presber
  • 6,429
  • 2
  • 37
  • 66
Tomasz Bubała
  • 2,093
  • 1
  • 11
  • 18
0

The way you call the setter is not correct:

let person = {
  _name: 'Lu Xun',
  _age: 137,

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

};
console.log(person.age = 'bdhh');
console.log(person.age = 13);
messerbill
  • 5,499
  • 1
  • 27
  • 38
0

Your calling method is not correct

let person = {
  _name: 'Lu Xun',
  _age: 137,

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

};
console.log(person.age = 'bdhh');
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68