Object JS - Challange

Challange link here

Hello everyone who’s reading this article!!!

After an hour of puzzling over this challenge, I took a break by cooking dinner, having a glass of red wine and accidentally happened to be present in front of the TV while Selling Sunset on Netflix was on, btw, don’t watch it, your IQ will drop by 50 points unless you have at least the one glass of wine in already.
I came back to this cause I just could not sleep calmly.

The code is working, nevertheless, I am a little bit confused about its functionality.
How come the function remembers its argument long after it was invoked? Like firstAndLast in this example.

PS. I had not known about local variables in constructor functions before I started so I tried to figure it out my way and happed to witness it stored the argument while debugging. I’d do it this way now.
Code here:

var Person = function(firstAndLast) {
  // notice I do not store firstAndLast in any variable below
  this.getFirstName = function() {
    if(this.firstName === undefined){
       this.setFirstName(firstAndLast.split(" ")[0])
    }
    return this.firstName
  };
  this.getLastName = function() {
    if(this.lastName === undefined){
       this.setLastName(firstAndLast.split(" ")[1])
    }
   return this.lastName 
  };
  this.getFullName = function() {
    if(this.firstName === undefined){
       this.setFirstName(firstAndLast.split(" ")[0])
    }
    if(this.lastName === undefined){
       this.setLastName(firstAndLast.split(" ")[1])
    }
    return this.firstName+" "+this.lastName
  };
  this.setFirstName = function(first) {    
    this.firstName = first
  };
  this.setLastName = function(last) {
    this.lastName = last
  };
  this.setFullName = function(firstAndLast) {
    this.setFirstName(firstAndLast.split(" ")[0])
    this.setLastName(firstAndLast.split(" ")[1])
  };

  return firstAndLast;
};

var bob = new Person('Bob Ross');
console.log(bob.getFullName());
var john = new Person('John Doe');
console.log(john.getFullName());

Is it going to remember that argument forever?

Thank ya’ll

Is it because it creates variable which is within the function’s scope so whenever I use it within the function it’s addressed to it?

You’re quite right. It basically just creates a whole new variable using the Constructors and the objects in it. So essentially, the var bob has it’s own object and values. It just copies it from the original Constructor. It’s like copy pasting, but in codes.

your solution has one issue: you should not be creating this.lastName and this.firstName as it requires to have only the six methods on the object