Rob F
Rob F

Reputation: 539

Returning a function from a function without calling the returned function

Can it be done?

I have the following:

populate.func = function(node, state) {
    return (new function(data) { /* stuff*/ });
}

However, calling populate.func invariably calls both the called function and the returned function, whether I use regular calling convention or the call method. I want to be able to call populate.func and just return the function as a value to make use of, without actually running it...

Upvotes: 0

Views: 349

Answers (2)

Willem D'Haeseleer
Willem D'Haeseleer

Reputation: 20180

just do this

populate.func = function(node, state) {
    return (function(data) { /* stuff*/ });
}

Your code is instantiating a new object using your function as a constructor function, but you want to return a reference to your function instead, so just drop the new keyword.

Upvotes: 1

Vatev
Vatev

Reputation: 7590

I'm not even sure what your code is actually doing, but I think what you want is:

populate.func = function(node, state) {
    return function(data) { /* stuff*/ };
}

Upvotes: 0

Related Questions