How do I properly use the syntax for a setter method?


#22

the typical format is
if this then that
else, then this happens
This is the psuedo code I’m using to make me a little less confused. still reading your comment

I also said that I opened and closed it in that line, not just closed it.

I see what you’re saying; I just mislabeled it!


#23

yes, you had too many }.


#24
let person = {///opening of objects
  _name: 'Lu Xun',
  _age: 137,
  set age(newAge){///openoing of setter
    if (typeof newage === 'number') 
    {///beginning of then statement
      this._age = newage;
    } ///end of then statement
    
    ///should I put a semicolon at the end of the if then statement?

  
  else {
    console.log('Invalid input') ///beginning and closing of then statement; then statement closing is after return
    return "Invalid input"} ///closing of then statement
       
}///closing of setter, is this where it goes?
} ///closing of objects

#25

your if and else still use different curly brackets style, this is what i mean with things not coming across.

do:

if (typeof newage === 'number') 
{///beginning of then statement
    this._age = newage;
} ///end of then statement
else 
{
    console.log('Invalid input') ///beginning and closing of then statement; then statement closing is after return
    return "Invalid input"
}

or:

if (typeof newage === 'number') {///beginning of then statement
    this._age = newage;
} ///end of then statement
else {
    console.log('Invalid input') ///beginning and closing of then statement; then statement closing is after return
    return "Invalid input"
}

this curly brackets style should be one of Romes building blocks, its not the entire city.

here:

}///closing of setter, is this where it goes?

not the right indent level. Come on, you can do better :slight_smile:


#26
let person = { ///opening of objects
  _name: 'Lu Xun',
  _age: 137,
  set age(putIn)
  {///opening of setter
    if (typeof newage === 'number') 
  {///beginning of then statement
      this._age = newage;
  } ///end of then statement
    
    ///should I put a semicolon at the end of the if then statement?

  
 else {
    console.log('Invalid input') ///beginning and closing of then statement; then statement closing is after return
return "Invalid input"} ///closing of then statement
       
  }///closing of setter, is this where it goes?
  }; ///closing of objects   

I have changed the indent style to being all vertical. Is this what you meant?


#27

Im still getting an error message :expressionless: I literally went line by line. COuld you offer any suggestions?


#28
set age(putIn)
  {///opening of setter
    if (typeof newage === 'number') 
  {///beginning of then statement
      this._age = newage;
  } ///end of then statement

where did you define newage? the parameter (putIn) is what is holding the value, its what you should use when setting this._age and in the if comparison

your choice what you change, i would change the parameter if i where you

This is the last suggestion, after this, i am done

otherwise check my explanation about setters here:

Learn JavaScript: Objects Team Stats


#29

This should also be easy for you to explain because of your experience. So yes, it’s hard and everything else. This site is for learning how to code. If i knew how already, then I would be doing something else.


#30

I explained it perfectly in the topic i linked to? It explains how setters work, it should be possible to deduce from this that the parameter receives the value which we need to use within our method (comparison and setting _age property.

my only experience is learning things myself, figuring them out as i go along. And that is still what i am doing.


#31

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