-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
  • 34,922
  • 8
  • 55
  • 102
Danny
  • 183
  • 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,167
  • 2
  • 32
  • 63
Tomasz Bubała
  • 1,985
  • 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,142
  • 1
  • 23
  • 35
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,305
  • 8
  • 35
  • 64