1

I am aware of the fact that functions in JavaScript lead a dual life first of a function (as first class thing to create instances from) and the second one of a normal object.

But I am surprised to see the output of the following console.

function A() {
    console.info("A");
}
console.info(A.prototype.constructor === A.constructor); // false

I expected it to be true as I was not expecting constructor property on the object A as it's own property. And hence following the prototypical chain lookup it should have been the same object as A.prototype.constructor. Where am I wrong or what piece am I missing?

1
  • 1
    The first prototype used in A is Function.prototype because A is constructed from Function. Instances of A (aInstance = new A) will use A.prototype as the first prototype in its prototype chain because aInstance has A as it's constructor. More on prototype can be found here: stackoverflow.com/a/16063711/1641941 Commented Apr 24, 2015 at 11:59

2 Answers 2

5

Where am I wrong or what piece am I missing?

That A does not inherit from A.prototype. A is a (constructor) function, and inherits from Function.prototype. Do a console.log(Object.getPrototypeOf(A)) :-)

From A.prototype only new A instances do inherit (whose .constructor is A). See also __proto__ VS. prototype in JavaScript.

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

Comments

1

That is beacuse both are returning different values.

  • Object.prototype.constructor

    Returns a reference to the Object function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.

  • Function constructor

The Function constructor creates a new Function object. In JavaScript every function is actually a Function object.

Also if you console above code you will see

console.info(A.prototype.constructor); outputs

function A() {
    console.info("A");
}

console.info(A.constructor); outputs

function Function() { [native code] } // both are different.

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.