0

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...

Rob F
  • 531
  • 5
  • 17
  • why `new`? it should work without it. i mean, say `infunc = function(data){ /* stuff*/ };` You do: `return infunc;` – Prasanth Aug 07 '12 at 06:03
  • You want to return the function as a string? – me_digvijay Aug 07 '12 at 06:03
  • New because I read that this was a trick to pass a function by reference and pass variable in the state it was in when you passed the function as an argument, and not when it gets called. I think it does this because so long as a pointer is maintained to the child function, the parent can't be garbage collected either, nor its call stack. Or something. – Rob F Aug 07 '12 at 06:09
  • @RobF you have been terribly misinformed, go read up some more on javascript... – Willem D'Haeseleer Aug 07 '12 at 06:10

2 Answers2

1

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.

Willem D'Haeseleer
  • 17,802
  • 6
  • 58
  • 92
  • That seems to have worked. Will wait and experiment a bit before leaving feedback. Why did new operator cause it to call both functions? – Rob F Aug 07 '12 at 06:12
  • because you executed the function to instantiate an object from it, using the function as a constructor function. – Willem D'Haeseleer Aug 07 '12 at 06:13
  • That doesn't answer the question. – Rob F Aug 07 '12 at 06:19
  • @RobF It does, that is exactly the reason why it calls the function. You should read up on what [new](http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript) does in JS. – Vatev Aug 07 '12 at 06:22
  • @RobF do you feel like you understand what i tried to say or is it still unclear ? – Willem D'Haeseleer Aug 07 '12 at 06:36
  • @helmus: It's clear now. I thought the new operator used the return value of the function rather than the function itself. I also didn't catch on that the constructor was in fact, that; I thought it was a definition of one specific function (thinking it analogous to struct { blah;} var; in C ), hence my feeling the need for the new operator to make copies of it. Thanks for the help. – Rob F Aug 07 '12 at 06:46
0

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*/ };
}
Vatev
  • 7,017
  • 1
  • 28
  • 37
  • @RobF adding in new doesn't make any diffrence there, you are returning an object by reference, instantiated by your constructor function, but not the function it self. – Willem D'Haeseleer Aug 07 '12 at 06:08
  • `return function...` will return a reference to the function w/o executing it. – Vatev Aug 07 '12 at 06:11