2

function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
console.log(myFather instanceof person); //true
console.log(myFather instanceof Object); //true
console.log(myFather instanceof Function); //false

Hello, in this case, we created an object from the function constructor: 'person'.

Every function in JavaScript is an instance of the Function constructor. Why is myFather not an instance of Function?

thelearner
  • 1,440
  • 3
  • 27
  • 58
  • 2
    Because the object referenced by `myFather` is a plain object *created* by the `person()` constructor. When you call a function with `new`, a new object is created and bound to `this` in the constructor context. – Pointy Jul 08 '17 at 12:59
  • The prototype chain is myFather -> person.prototype -> object.prototype . instanceof checks for that – Jonas Wilms Jul 08 '17 at 13:17

1 Answers1

3

myFather is an object instance of person that is why it is returning true for myFather instanceof Object but false for myFather instanceof Function as it is not a function but an object, you can't call myFather again to instantiate another object. Actually person is an instance of Function. When you call new person a plain object is returned and it is stored in myFather.

function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
var myFather = new person("John", "Doe", 50, "blue");
console.log(myFather instanceof person); //true
console.log(myFather instanceof Object); //true
console.log(myFather instanceof Function); //false
console.log(person instanceof Function); //true
Dij
  • 9,761
  • 4
  • 18
  • 35