Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating functions using `Object.create`

Tags:

javascript

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?

like image 556
Just Alex Avatar asked Oct 16 '25 16:10

Just Alex


1 Answers

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:

  1. to use the usual function-object creation syntaxes (eg. function() {}, () => {}), or
  2. Use the Function constructor, or
  3. Use classes and extend Function.

const f = Object.create(Function.prototype)
console.log(typeof f) // 'object' (not 'function') 
like image 197
Ben Aston Avatar answered Oct 18 '25 05:10

Ben Aston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!