Query about arrow function on Make a Person

Tell us what’s happening:

Just a query on syntax. I’m still solving the challenge. Okay. Here is my query:

I initially tried to use arrow functions but the got back errors. I don’t quite understand why I would get that. I’m not sure if I received errors because I’m just not writing it out correctly or if there is something about arrow functions that are not appropriate to this particular scenario. Would some one please clear this up for me? Thank you

Your code so far


var Person = function(firstAndLast) {

  //getters
this.first = firstAndLast.split(' ')[0];
this.last = firstAndLast.split(' ')[1];
this.fullName = this.first + ' ' + this.last;
  //setters
this.setFirstName = function(str){
  this.first = str;
},
this.setLastNam = function(str){
  this.last = str;
},
this.setFullName = function(str){
  this.fullName = firstAndLast;
}
}

var bob = new Person('Bob Ross');
bob.getFullName();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person/

Show us the code with your arrow functions :slight_smile:


var Person = function(firstAndLast) {

  //getters
this.first = firstAndLast.split(' ')[0];
this.last = firstAndLast.split(' ')[1];
this.fullName = this.first + ' ' + this.last;
  //setters
this.setFirstName = ((str)=>{
  this.first = str;
}),
this.setLastName = ((str)=>{
  this.last = str;
}),
this.setFullName = ((str)=>{
  this.fullName = firstAndLast;
})
}

var bob = new Person('Bob Ross');
bob.getFullName();

No need to wrap the arrow function into outer brackets. Lose the brackets and you should be fine.

1 Like

@bananahair With your current code, the reason you are getting an error is because you have the following call to a “getFullName” method of the bob object which does not exist.

bob.getFullName();

As @NickEU pointed out, you do not to wrap the function in the extra set of parentheses. However, it does not cause an errors by leaving them in place.

4 Likes