0

I know this may be a bizarre question with possibly no practical application, but would it be possible to make a JavaScript class that constructs instances that behave as functions? Here's what I mean:

function Factory() {}

// this may not be necessary, but I'll include it for sake of clarification
Factory.prototype = Object.create(Function.prototype);

var method = new Factory();

method(); // Objective: should not throw TypeError

To further clarify the objective:

  • method should be callable as a function
  • method should be the result of calling a constructor (e.g. var method = new Factory() in this case)
  • The constructor cannot be Function.
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • What problem are you trying to solve? You've posted some code with an unknown application. We have zero context in which to evaluate it or know what else to suggest. Also, it's a bit hard to tell what your question is too. – jfriend00 Jul 12 '15 at 23:08
  • @jfriend00 it's just a challenge with essentially no context. I'm just curious if it's possible to achieve this in JavaScript, and I think answers to this might reveal some helpful niches of various JavaScript constructs to make behavior like this possible. – Patrick Roberts Jul 12 '15 at 23:10
  • So, by "is it possible", do you just mean that it creates no error? You haven't described what you want an output to be, so it's hard to evaluate whether an unknown result is possible. Sorry, but I have no idea how to answer a question "is it possible", when the "it" is not described at all. In fact, now I don't even really know what the question is. – jfriend00 Jul 12 '15 at 23:12
  • @jfriend00 essentially, the objective is to make `method` able to be called as a function, and have it as the result of calling a constructor that's not `Function`. – Patrick Roberts Jul 12 '15 at 23:16
  • possible duplicate of [Invoking a function Object in Javascript](http://stackoverflow.com/questions/1920163/invoking-a-function-object-in-javascript) – IS4 Jul 12 '15 at 23:16
  • A constructor can return anything you want. If you want to just return another function, you can do that. – jfriend00 Jul 12 '15 at 23:17
  • @IllidanS4 that's related but this question is certainly not a duplicate of that. – Patrick Roberts Jul 12 '15 at 23:24
  • @Patrick Ah, that question indicated you can't return functions. Looks like you can. – IS4 Jul 12 '15 at 23:34

1 Answers1

3

If I understand correctly. Object constructor need to return his method. Then you can call it like you describe.

function Factory() {
    return this.method;
}

Factory.prototype.method = function() {
    console.log('from method');
};

var method = new Factory();

method();

http://jsfiddle.net/ydcoL3c2/

Jędrzej Chałubek
  • 1,410
  • 1
  • 16
  • 29
  • Wait, so you can return a value from a constructor? I did not know this was possible. – Patrick Roberts Jul 12 '15 at 23:15
  • Yeah, you can return anything u need. Basically it is still function. – Jędrzej Chałubek Jul 12 '15 at 23:16
  • I thought that if you called any function as a constructor (i.e. with `new` keyword) that the function was not allowed to return a value, or else it would throw an error. I'm surprised that this is allowed. – Patrick Roberts Jul 12 '15 at 23:20
  • @PatrickRoberts - if you return something from a constructor invoked with `new`, then the object that was created via `new` is not returned and, if not assigned elsewhere in the constructor, will just be garbage collected. – jfriend00 Jul 12 '15 at 23:26
  • Well I guess I learned something new. Thanks for the insight. – Patrick Roberts Jul 12 '15 at 23:28