Make A Person: instanceof issue

Hi, there is something wrong with my advanced algorithm and I cannot seem to figure out what. I haven’t looked at any solutions for this and I’m trying my best not to. I’ve got every one of the tests of this algorithm right except where “bob instanceof Person” should return “true”; I just don’t understand what is wrong with my code. I’m sure it’s something blatantly obvious. Please help. Thanks.


var Person = function(firstAndLast) {
    var arr = firstAndLast.split(" ");
    return {
      getFirstName: function(){
        return arr[0];
      },
      getLastName: function(){
        return arr[1];
      },
      getFullName: function(){
        return arr.join(" ");
      },
      setFirstName: function(val){
        arr[0] = val;
        return arr;
      },
      setLastName: function(val){
        arr[1] = val;
        return arr;
      },
      setFullName: function(val){
        newArr = val.split(" ");
        arr[0] = newArr[0];
        arr[1] = newArr[1];
        return arr;
      } 
    };
};

var bob = new Person("Bob Ross");
console.log(Person instanceof Object); //returns true
console.log(Person instanceof Function); //returns true
console.log(bob instanceof Object); //returns true
console.log(bob instanceof Person); //returns false

It’s hard to help without giving away the answer, but you’re currently writing a plain ol’ function instead of an Object constructor. Here’s an example of an Object constructor from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Using_a_constructor_function

I wish I could explain it better but I need more work on these concepts too.

1 Like

What you are returning from the Person function is an anonymous object.

var Person = function(name) {
  this._name = name

  this.getName = function () {
    return this._name
  }
};

var bob = new Person("Bob Ross");
console.log(Person instanceof Object); //returns true
console.log(Person instanceof Function); //returns true
console.log(bob instanceof Object); //returns true
console.log(bob instanceof Person); //returns **true**
console.log(bob.getName())
1 Like

Thanks a lot guys, that’s sorted it for me. I get what I was doing wrong. Cheers dhcodes and gusth7k! Keep doing what you do, people like you inspire me to keep on keepin’ on.

1 Like