I learnt that any function type object has property prototype.
For example:
Objecthas propertyprototypeFunctionhas propertyprototypePersonhas propertyprototype
But,
> typeof Function.prototype
"function"
I have two questions,
1) why function type object Function.prototype does not have its own property prototype, in the below visualisation?
2) Any object usually inherits from an object type object, but in the below visualization, Function object and Person object inherit from function type object Function.prototype?
So, For above two questions, Is it more safe to have Function.protoype as object type?
enter image description here
2 Answers 2
Why function type object
Function.prototypedoes not have its own propertyprototype, in the below visualisation?
Why would it, what would you expect it to point to? It's not a constructor function. (In fact, with ES6, many functions don't have a .prototype method).
It's rather quite weird that Function.prototype is a callable object (function), there's actually no benefit from that. It could've been a plain object just as well. "The spec said so" is all we have (see also these three questions).
Any object usually inherits from an object type object, but the
Functionobject andPersonobject inherit from function type objectFunction.prototype?
Well yes, why wouldn't they? As you say yourself, functions are just objects, and every object inherits from another object (or null). That some objects are callable (and typeof yields "function" for them) does not make any difference for inheritance.
Comments
If you compare the prototypes of the common types in JavaScript you can see that their prototype is an instance of the actual type:
Function.prototype = function () {}
Array.prototype = []
Object.prototype = {}
I am not sure why that is, but i guess that is an ECMA-standard or it is a thing which Browser implement for themself so everything makes sense. In other words how would you know a Function is a Function when the prototype of Function is an object like the prototype of Object. English isn't my mother tongue but I hope I made my point clear.
But to answer your question: I guess the prototype of Function is an exception and has no own prototype, because it is not needed.
2 Comments
Function.prototype as object type object, to resolve my two questions? Are there any issues in doing that?Explore related questions
See similar questions with these tags.