In js we have a function named “Function“.
An instance of it returns a function:
var myfunc = new Function('arg1','arg2','return arg1+arg2');
In that example the variable myfunc contains a function that returns the sum of two given params.
My question is – how is that possible that Function is a function? It can’t be an instance of itself.
And Object is a function too, an instance of Function. But Function is an instance of Object because function are objects.
And I can’t understand how is it possible, it’s an infinity loop…
Thanks.
The trivial answer is “because ECMA-262 says so”.
Objects like Function, Object, Array, Date etc. are built–in objects so they aren’t constructed in the usualy way, they just are. The relationship between them, their prototype and their
[[Prototype]]is established by the environment in accordance with ECMA-262. So Function inherits from its own prototype (i.e.Function.prototype === Function[[Prototype]]), which inherits from Object.prototype.