javascript - Why does bob instanceof Person return false? -


i need explain me why bob instanceof person returns false code:

var person = function(firstandlast) {      var names = firstandlast.split(' ');      return {          getfirstname:  function() {           return names[0];         },          getlastname: function() {           return names[1];         },          getfullname: function() {           return names[0] + " " + names[1];         },          setfirstname: function(first) {           names[0] = first;         },          setlastname: function(last) {           names[1] = last;         },          setfullname: function(firstandlast) {           names = firstandlast.split(' ');         }      };    };  var bob = new person('bob ross'); 

i've tried looking answer couldn't find anything. far understand bob instanceof person should return true doesn't.

the problem you're returning object constructor. object not object initialized new, it's not instance of person class. should assigning this.propertyname in constructor, , let return default value.

var person = function(firstandlast) {      var names = firstandlast.split(' ');        this.getfirstname = function() {      return names[0];    };      this.getlastname = function() {      return names[1];    };      this.getfullname = function() {      return names[0] + " " + names[1];    };      this.setfirstname = function(first) {      names[0] = first;    };      this.setlastname = function(last) {      names[1] = last;    };      this.setfullname = function(firstandlast) {      names = firstandlast.split(' ');    };    };    var bob = new person('bob ross');    console.log(bob instanceof person);


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -