0

I created a person class. When I instantiate it as the daffyDuck instance, it is not recognized as an instance of A_person. Why not?

var A_person = function(firstAndLast) {
  var splitName = firstAndLast.split(" ");
  return {
    getFullName: function(){
      return splitName.join(" ");
    } 
  };
};

var daffyDuck = new A_person('Daffy Duck');
daffyDuck instanceof A_person  // false (I expected this to be true!)
kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • You're returning a `different object` from your `constructor`, which is definitely not a `instance of` A_person. That is what I can think of. Try returning `this` and check it out. – Vedant Terkar Jan 01 '16 at 06:57

3 Answers3

2

I think you intend to use a prototype instead of a constructor (returning an object)... something like this

function A_Person(firstAndLast){
    this.splitName = firstAndLast.split(" ");
}
A_Person.prototype.getFullName = function(){
     return splitName.join(" ");
}

For more information, see here https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Arhowk
  • 901
  • 4
  • 11
  • 22
  • 1
    W3schools is a terrible resource. Use this one: https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain – John Dvorak Jan 01 '16 at 07:13
  • "All JavaScript objects inherit their properties and methods from their prototype." - except Object.prototype. Acceptable simplification. Semi-corrected a few sentences later. – John Dvorak Jan 01 '16 at 07:14
  • `All JavaScript objects (Date, Array, RegExp, Function, ....) inherit from the Object.prototype. ` - except for `Object.create(null)`. Outdated information at best, since `Object.create` is ES6. – John Dvorak Jan 01 '16 at 07:16
  • "The constructor function is the prototype for your person objects." - blatanty false. The function's `prototype` property is what gets used. – John Dvorak Jan 01 '16 at 07:17
  • "Sometimes you want to add new properties (or methods) to ..." - terrible idea, times three. Monkey-patching is acceptable in Ruby, but even then better solutions exist. Monkey-patching is way too risky in Javascript. And changing a class behavior dynamically goes against the principles of OOP. – John Dvorak Jan 01 '16 at 07:18
  • "You cannot add a new property to a prototype the same way as you add a new property to an existing object, because the prototype is not an existing object." - blatantly false. While `Person.prototype.nationality =` is a bad idea, that's different from not being possible, and the stated reason is nonsense. – John Dvorak Jan 01 '16 at 07:21
  • "To add a new property to a constructor, you must add it to the constructor function:" - should. But it's also possible to add properties dynamically, and Chrome _still_ can use hidden classes for objects created this way. – John Dvorak Jan 01 '16 at 07:22
1

You aren't defining a class at all. You're just creating a method and saving it to a variable.

You need to actually define the class. Starting with ES6 this is super easy - (learn more here):

class A_person {
  constructor(firstAndLast) {
    var names = firstAndLast.split(" ");
    this.firstName = names[0];
    this.lastName = names[1];
  }
}

var daffyDuck = new A_person('Daffy Duck');
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • 1
    Be warned when using this approach when you intend on deploying to clients though as this standard was just approved in June. Many computers, primarily in the business world, will have unupdated software and will not be able to run this code. This is probably just a learning project (or school) anyway, so I don't think it will be an issue. – Arhowk Jan 01 '16 at 07:05
  • @Arhowk super true. If OP wants to use this method you should for sure transpile to ES5. – Matthew Herbst Jan 01 '16 at 07:06
1

You can remove the return statement and use this instead, to attach the function to objects.

var A_person = function(firstAndLast) {
  var splitName = firstAndLast.split(" ");
  this.getFullName = function(){
      return splitName.join(" ");
    } 
};

var daffyDuck = new A_person('Daffy Duck');
daffyDuck instanceof A_person  // true
Ankit
  • 1,471
  • 17
  • 29