3

I am looking for a way to construct a function that has some inheritance.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype

What I am looking for is an object that is a function that can be instantiated in a program, but that has new properties attached to it's prototype.

So, what we shouldn't do is this:

Function.prototype.x = function(){

};

Function.prototype.y = function(){

};

so what I want to do is something like:

const proto = Object.create(Function.prototype);

proto.x = function(){};

proto.y = function(){};

however, at some point later in the program, I would like to actually define the function body for proto, so that I can actually call proto like a function:

proto();

is there a way to do this? The above definitely does not work :)

This is a little closer, and the error message is telling:

function F(){

}

F.prototype = Function.prototype;

var f = new F();

f.apply(null);

Run the above and we get:

TypeError: Function.prototype.apply was called on [object Object], which is a object and not a function at Object. (/Users/amills/WebstormProjects/oresoftware/suman/exp7.js:15:3) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) at Function.Module.runMain (module.js:575:10) at startup (node.js:160:18) at node.js:449:3

13
  • I've tried this, the further you go down this road, the more hacky it gets. What do you want to achieve with that? Why does the instance have to be a function? why can't you simply attach the function to the instance? Commented Jun 4, 2016 at 0:31
  • I believe you, even given the little amount of experimentation that I have done - I am creating a library/API. Inheritance just means more performance, because prototypes are shared objects - what I want is an object that is a function but whose prototype is shared, but is not the function prototype :) probably not going to happen :( Commented Jun 4, 2016 at 0:35
  • In compatible engines, you can define a class that extends Function (support). Commented Jun 4, 2016 at 0:39
  • 1
    @AlexMills A function is recognized by an internal property, [[Call]]. This isn't inherited from a prototype and Object.create() doesn't have any mechanism to define it. – class works by invoking the Function constructor through super(), which does set [[Call]]. Commented Jun 4, 2016 at 0:49
  • 1
    A possible alternative could be to change the function's prototype after it's been created – var foo = function () {}; Object.setPrototypeOf(foo, proto);. Commented Jun 4, 2016 at 0:53

1 Answer 1

1

The right way to do this is as follows!

step 1

create an Object which inherits from Function.prototype

const obj = Object.create(Function.prototype);  // inherits many properties, etc

step 2

later in your program declare the function

const f = function(){};

step 3

set the __proto__ of f to obj

Object.setPrototypeOf(f,obj); which is basically equivalent to f.__proto__ = obj;

....this seems to work without any problems, thanks for everyone's help!

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

4 Comments

note: this works, except you will not be able to call f.apply() or f.call() or f.toString(), so that will probably be a problem.
Please just only show Object.setPrototypeOf and don't even mention the deprecated __proto__ :-)
@AlexMills Then make obj inherit from Function.prototype
@Oriol thanks, I tried that, and it worked perfectly, good call

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.