Search code examples
javascriptfunctionfunction-constructor

If we create an object called 'a' from a function constructor, then why is 'a' not an instance of Function?


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?


Solution

  • 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