JavaScript Objects - Getters and Setters III


#1

Hello. My code is not running. Can someone tell me what I’m doing wrong? Thanks! Kate

  _name: 'Lu Xun',
  _age: 137,
  
  set age(ageIn) {
    if (typeof ageIn === 'number') {
      this._age = ageIn;
    }
    else {
      console.log('Invalid input');
      return 'Invalid input';
    }
  }

  get age() {
  console.log(`${this._name} is ${this._age} years old.`);
	return this._age;
}
};

person.age = 'Thirty-nine';
person.age = 39;

#2

Setters do not often need to return anything. The setting is on the instance variable, or error message logged out.

Is your code missing the opening line:

const person = {

#3

Hello. I’m still getting an error message. Unable to pinpoint the message.

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';
    }
  }

  get age() {
  	console.log(`${this._name} is ${this._age} years old.`);
		return this._age;
	}
};

person.age = 'Thirty-nine';
person.age = 39;

#4

Should there be a comma before get age?

Setters and Getters III


#5

Sorry, I don’t understand.


#6

properties and methods of an object should be separated by a comma (,), you miss a comma somewhere


#7

I’ve added a console.log statement to line 8 of my code, and 39 has been added to the output. However, I’m still unable to clear the challenge. What might I be doing wrong? Thanks!

let person = {
  _name: 'Lu Xun',
  _age: 137,
  
  set age(ageIn) {
    if (typeof ageIn === 'number') {
      this._age = ageIn;
      console.log(this._age);
    }
    else {
      console.log('Invalid input');
      //return 'Invalid input';
    }
  },

  get age() {
		return this._age;
  	console.log(`${this._name} is ${this._age} years old.`);	
	}
};

person.age = 'Thirty-nine';
person.age = 39;

#8

read the instructions carefully:

Use dot notation inside of a console.log() to get the value saved to your _age property and print it to the console.

where should the console.log be placed? I can tell you that your current placing of console.log is incorrect

we want to print the value of age property, where should this be done? After set an age for the person.


#9

Anything after return is unreachable. If you are required to, or intend to log, then do it before the return, not after.


#10

I switched the console.log statement with the return statement. The feedback ‘Did you use dot notation to get the value saved to the _age property?’ appeared below the code. I don’t see anything wrong with my dot notation. What might I be missing? Thanks!

let person = {
  _name: 'Lu Xun',
  _age: 137,
  
  set age(ageIn) {
    if (typeof ageIn === 'number') {
      this._age = ageIn;
      console.log(this._age);
    }
    else {
      console.log('Invalid input');
      //return 'Invalid input';
    }
  },

  get age() {
    console.log(`${this._name} is ${this._age} years old.`);
		return this._age;
	}
};

person.age = 'Thirty-nine';
person.age = 39;

#11

It is not common to log out the value when setting it. There could be an issue with that line being present.


#12

Hey, hi there ksirgey, stetim94 and mtf.

It could be not relevant any more, I’ve stuck on the same.
I guess I understand ksirgey’s issue.

The issue, why your code is not logging in to the console is because you’re not calling the proper variable.
We’ve set up “getter” according to guidelines to store information inside the age.
(take a look->

codeacademypostSnG14

so we need to call getter, therefore ->

Hope that would help.
Have a great day.

Cheers everyone.


#13

I’ve not used return at the getter and it still worked just look at screen


#14

The purpose of a getter is to return the attribute’s value, not specifically output to the display. Ideally, a getter will not log anything, only return.

Consider,

person = {
    _name: 'Bill',
    _age: 29,
    get age () {
        return this._age;
    },
    set age (newAge) {
        this._age = newAge;
    },
    repr () {
        console.log(`${this.name} is ${this.age} years old.`);
    }
};

person.repr()    //  undefined is 29 years old.

See how the getter retrieves the age, but in the absence of one (name getter), not the name?

person = {
    _name: 'Bill',
    _age: 29,
    get name() {
        return this._name;
    },
    get age () {
        return this._age;
    },
    set age (newAge) {
        this._age = newAge;
    },
    repr () {
        console.log(`${this.name} is ${this.age} years old.`);
    }
};

person.repr()    //  Bill is 29 years old.

#15

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