Make a Person: How is “firstAndLast” being stored (this)

Make a Person: How is “firstAndLast” being stored (this)
0

#1

Hi all,

I am referring to this challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person

Here my code:

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly

  this.getFullName = function() {
    return firstAndLast;
  };

  this.getFirstName = function(){
    return firstAndLast.split(' ').shift();
  }

  this.getLastName = function(){
    return firstAndLast.split(' ').pop();
  }

  this.setFirstName = (firstName) => {
    let lastName = firstAndLast.split(' ')[1];
    firstAndLast = firstName.concat(' '.concat(lastName)); 
  }

  this.setLastName = (lastName) => {
    let firstName = firstAndLast.split(' ')[0];
    firstAndLast = firstName.concat(' '.concat(lastName)); 
  }

  this.setFullName = (newFullName) => {
    firstAndLast = newFullName;
  }

  return firstAndLast;
};

Here my questions:

  1. After having instantiate a new Person object the firstAndLast argument stores the full name of our new object right? So given that, then there is always a kind of firstAndLast variable per instantiated Person object?

  2. It is possible to access firstAndLast from outside the object?

  3. As I did in my code I’m able to mutate firstAndLast even though it was not saved in another internal object variable like:

this.firstAndLast = firstAndLast;
  1. Although I haven’t save firstAndLast in another variable as shown above I use firstAndLast all over as an instance specific variable. How does it work?

I hope I used the correct words to formulate my questions.
As always thanks in advanced for any advice.

Regards,


#2

Yes

No, because it is local to the Person function

firstAndLast is a variable local to the Person function. this.firstAndLast is a property accessible outside the Person function.

I think I answered this question with my response to the third question.


#3

@RandellDawson
Thanks for your answer!