6

The first question I was asked in the interview, and to be honest I saw myself really confused and shown the door,

Consider the snippets:

Case a:

    var sayHello = new Function("alert('Hello there');");
    alert(sayHello instanceof Function);            // true 
    alert(sayHello instanceof Object);              // true, since every 
                                                    // object inherits from Object

Case b:

    var myFunction = function(){

    }

    var ins = new myFunction();              
    alert(ins instanceof myFunction);        // ofcourse, true
    alert(myFunction instanceof Function);   // true

    alert(ins instanceof Function);          // false, Why is this not true?

As per my understanding, Function must be in the prototypical chain of ins?

ins <-- myFunction <-- Function <-- Object
4
  • Well, ins is not even a function Commented Sep 3, 2015 at 6:34
  • 1
    ins is an object, not a function, you can't call ins() here :) Commented Sep 3, 2015 at 6:35
  • @Wouter : But at that end, every function must inherit from Object. Commented Sep 3, 2015 at 6:39
  • 2
    @ShirgillAnsari: every function inherits from Object, true, but not every object is a function. In this case, it isn't. myFunction is a constructor function, calling new on it creates a new object that is not a function. Commented Sep 3, 2015 at 6:40

4 Answers 4

10

You seem to misinterpret the new here in Javascript.

The

new myFunction()

doesn't create a new instance of the function. Rather it creates a new object which inherits from myFunction.prototype and calls myFunction, passing the object as this to the function.

Thus, you haven't really created a new instance of the function, your ins is not a function. You can easily verify it by trying to pretend it is:

var myFunction = function(){

}

var ins = new myFunction(); 

ins(); <-- error, ins is not a function

Since it is not a function, why would you expect the Function to be in its prototype chain?

Sign up to request clarification or add additional context in comments.

1 Comment

This provided me with sense of relief.
3

So, you have it close, but not quite. The prototypal chain is actually as follows:

ins <-- myFunction.prototype <-- Object

As you can see, the ins object inherits from the particular function prototype, and not from Function directly.

So, in essence ins is not an instance of a myFunction, so much as an instance of myFunction.prototype.

This can be more clearly be shown, by the fact that when you add functions to myFunction.prototype, they are attached to the instance of the object once it has been created.

For a great source of information on exactly what is happening, check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new This explains the behaviour fully.

2 Comments

@major-major: I disagree with the line that "So, ins is not an instance of a myFunction". It clearly is.
@ShirgillAnsari I'm simply trying to point out that myFunction.prototype is what ends in the prototype chain, and not myFunction directly
1

ins is not instanceof Function because you declare it like an instance (new myFunction). So, it's an instance of myFunction.

Just reference myFunction:

var myFunction = function(){};
var res = document.querySelector('#result');
var ins = myFunction;              
res.textContent = ['ins instanceof myFunction: ',ins instanceof myFunction,
                   '\nmyFunction instanceof Function: ', myFunction instanceof Function,
                   '\nins instanceof Function: ', ins instanceof Function].join('');
<pre id="result"></pre>

Comments

0

The trap you are falling into here is that you are assuming that the instanceof operator is transitive. You write

ins <-- myFunction <-- Function <-- Object

and conclude that it also holds that

ins <-- Function

It happens to be true that

myFunction <-- Object

but this is not due to a transitive instanceof.

In fact, the rule for instanceof is that if a instanceof b and b.prototype instanceof c it follows that a instanceof c but it is not required that b instanceof c. Look at this example:

function func() {}
function superFunc() {}
func.prototype = new superFunc();

var x = new func();

Now, x is an instance of func and superFunc, but func is not an instanceof superFunc. In other languages you would rather say that func is a subclass of superFunc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.