user3186555
user3186555

Reputation:

Access containing function

So, I'm working on a library that generates functions using a function constructor.

Each generated function is to have a property attached to the function known as default for store default values for an options parameter.

like so:

var generated_function = function( arg1, arg2, options_arg ){ ... }
generated_function.default = {}

The reason for attaching the default as a property of the function instead of storing it inside the function like this:

var generated_function = function( arg1, arg2, options_arg ){
  default = {}
}

is that I want the option to change the default settings during runtime if needed.

An example use would be say your default options is a huge object consisting of 100s or evens 1000s of properties, and your argumentative options is on average only 25% of the size that the default is.

Let's also say you are calling this function 100s or 1000s of times, and you are changing the argumentative options every call, but only changing the default options say a maximum of only 2 times.

Now I could just pass the default options along with the argumentative options in that parameter, but that would require creating and passing a lot of objects that are very large in size to the generated function. This is a memory problem.

I cannot afford memory problems with this script. Remember that the function is generated, meaning I also face the problem of passing all of data at that size and frequency to not one, but multiple functions

What I need is to be able to access that attached property, but I cannot do it like this:

var generated_function = function( arg1, arg2, options_arg ){ 
  ...
  generated_function.default ... //do something with it
  ...
}
generated_function.default = {}

because the name of the function is automatically generated meaning that it will be hard to access the function by name like I did, and the function can be generated inside another generated function meaning that I have to deal with scope problems.

So, basically what I am looking for is something along the lines of:

var generated_function = function( arg1, arg2, options_arg ){ 
  ...
  containing_function.default ... //do something with it
  ...
}
generated_function.default = {}

So I can use it like this in this generated scenario:

( window[ somerandomlygeneratedstring ] = function( arg1, arg2, options_arg ){ 
  ...
  containing_function.default ... //do something with it
  ...
  ( someobjectthatcannotbetracked[ somerandomlygeneratedstring ] = function( arg1, arg2, options_arg ){
    ...
    containing_function.default ...
    ...
  } ).default = { ... }

} ).default = { ... }

Upvotes: 1

Views: 39

Answers (2)

RobG
RobG

Reputation: 147523

Function expressions can have an optional name that is only available in the function, so:

var generated_function = function whatever( arg1, arg2, options_arg ){ 
  ...
  whatever.default ... //do something with it
  ...
}

It doesn't matter that all the generated functions have the same name.

There were some issues with named function expressions in IE up and including version 8 but they aren't an issue in this case if the name is chosen appropriately.

E.g.

var fn0 = function foo(){
  console.log(foo.props.bar);
}
fn0.props = {bar:'f0 old bar'};

var fn1 = function foo(){
  console.log(foo.props.bar);
}
fn1.props = {bar:'f1 old bar'};

// Call fn0, change props, call again
fn0();
fn0.props.name = 'f0 new bar';
fn0();


// Call fn1, change props, call again
fn1();
fn1.props.name = 'f1 new bar';
fn1();

The above shows that using the same name for multiple function expressions is fine since the name is only available within the function. It's practically identical to:

var fn0 = function () {
  var foo = arguments.callee;
}

except that it's safe in strict mode.

Upvotes: 0

JCOC611
JCOC611

Reputation: 19759

You can access the current function using arguments.callee. Here

var generated_function = function( arg1, arg2, options_arg ){ 
  ...
  arguments.callee.default ... //do something with it
  ...
}
generated_function.default = {}

Upvotes: 3

Related Questions