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.
8
  • 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. Commented Jul 12, 2015 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. Commented Jul 12, 2015 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. Commented Jul 12, 2015 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. Commented Jul 12, 2015 at 23:16
  • possible duplicate of Invoking a function Object in Javascript Commented Jul 12, 2015 at 23:16

1 Answer 1

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/

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

5 Comments

Wait, so you can return a value from a constructor? I did not know this was possible.
Yeah, you can return anything u need. Basically it is still function.
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.
@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.
Well I guess I learned something new. Thanks for the insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.