I am learning JavaScript and I am having trouble understanding a certain aspect of functions. For example, I know that I can create an array using Object.create
like this:
const array = Object.create(Array.prototype);
Since functions are also objects in JavaScript, I'm wondering if it's possible to create a function using Object.create in a similar way?
It is not possible.
Object.create
is merely a convenience function to configure prototype chains and define own properties.
The key distinguishing characteristic of functions is the existence of the non-userland [[Call]]
internal method, invokeable with the identifier(<arguments>)
syntax. The only ways I know to configure an object to have this internal method are:
function() {}
, () => {}
), orFunction
constructor, orFunction
.const f = Object.create(Function.prototype)
console.log(typeof f) // 'object' (not 'function')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With