Algorithm Scripting: Make a Person - Object/Constructor Question

I’m still getting used to objects and constructors, so this has me a bit confused.

My solution is correct, but I don’t totally understand at the concept level why firstAndLast is accessible after the object is created.

More specifically, how am I able to access the the firstAndLast property for the getter and setters after the object is created when there is no firstAndLast property defined on the object?

var Person = function(firstAndLast) {
  // Complete the method below and implement the others similarly
  this.getFullName = function() {
    return firstAndLast;
  this.getFirstName = function() {
    let spaceIndex = firstAndLast.indexOf(" ");
    return firstAndLast.substring(0,spaceIndex);    
    this.getLastName = function() {
    let spaceIndex = firstAndLast.indexOf(" ");
    return firstAndLast.substring(spaceIndex+1);    
   this.setFirstName = function(first) {
    firstAndLast = firstAndLast.replace(/\w*/,first);
  this.setLastName = function(last) {
    firstAndLast = firstAndLast.replace(/(\s)(\w*)/," " + last);
    this.setFullName = function(newName) {
    firstAndLast = firstAndLast.replace(/.*/,newName);


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

firstAndLast is not a property. It is the Person function’s argument (variable) and it is local to the Person function and all of the functions within the Person function have access to it.

Got it, so not a property. I’m still pretty mixed up in my head on this one. So for this code that calls the functions:

var bob = new Person(‘Bob Ross’);

Here’s my understanding and questions, let me know where I"m going off the rails:

(1) First Line - var bob is now an object of type Person
(2) Second Line - calls the function getFullName without any arguments. getFullName function references firstAndLast even though no argument is being passed. This is where I get confused. Where is the value from firstAndLast coming from?

You passed it into the Person function with the call Person(‘Bob Ross’); The string ‘Bob Ross’ is passed into the Person function and any function inside it has access to this argument which is named firstAndLast inside the Person function.

1 Like

Got it, that makes sense and helps but I think in general my understanding of constructors and objects is still a bit fuzzy. I’ll revisit that section again and see if I can connect the dots a little better.