Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function's default value?

Is there a way to retrieve a function's default argument value in JavaScript?

function foo(x = 5) {
    // things I do not control
}

Is there a way to get the default value of x here? Optimally, something like:

getDefaultValues(foo); // {"x": 5}

Note that toStringing the function would not work as it would break on defaults that are not constant.

like image 774
Benjamin Gruenbaum Avatar asked Oct 04 '15 14:10

Benjamin Gruenbaum


3 Answers

Since we don't have classic reflection in JS, as you can find on C#, Ruby, etc., we have to rely on one of my favorite tools, regular expressions, to do this job for us:

let b = "foo";
function fn (x = 10, /* woah */ y = 20, z, a = b) { /* ... */ }

fn.toString()
  .match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1] // Get the parameters declaration between the parenthesis
  .replace(/(\/\*[\s\S]*?\*\/)/mg,'')             // Get rid of comments
  .split(',')
  .reduce(function (parameters, param) {          // Convert it into an object
    param = param.match(/([_$a-zA-Z][^=]*)(?:=([^=]+))?/); // Split parameter name from value
    parameters[param[1].trim()] = eval(param[2]); // Eval each default value, to get strings, variable refs, etc.

    return parameters;
  }, {});

// Object { x: 10, y: 20, z: undefined, a: "foo" }

If you're going to use this, just make sure you're caching the regexs for performance.

Thanks to bubersson for hints on the first two regexs

like image 136
pilau Avatar answered Oct 16 '22 14:10

pilau


Is there a way to get the default value of x here?

No, there is no built-in reflection function to do such things, and it is completely impossible anyway given how default parameters are designed in JavaScript.

Note that toString()ing the function would not work as it would break on defaults that are not constant.

Indeed. Your only way to find out is to call the function, as the default values can depend on the call. Just think of

function(x, y=x) { … }

and try to get sensible representation for ys default value.

In other languages you are able to access default values either because they are constant (evaluated during the definition) or their reflection allows you to break down expressions and evaluate them in the context they were defined in.

In contrast, JS does evaluate parameter initializer expressions on every call of the function (if required) - have a look at How does this work in default parameters? for details. And these expressions are, as if they were part of the functions body, not accessible programmatically, just as any values they are refering to are hidden in the private closure scope of the function.
If you have a reflection API that allows access to closure values (like the engine's debugging API), then you could also access default values.

It's quite impossible to distinguish function(x, y=x) {…} from function(x) { var y=x; …} by their public properties, or by their behaviour, the only way is .toString(). And you don't want to rely on that usually.

like image 3
Bergi Avatar answered Oct 16 '22 13:10

Bergi


I'd tackle it by extracting the parameters from a string version of the function:

// making x=3 into {x: 3}
function serialize(args) {
  var obj = {};
  var noWhiteSpace = new RegExp(" ", "g");
  args = args.split(",");
  args.forEach(function(arg) {
    arg = arg.split("=");
    var key = arg[0].replace(noWhiteSpace, "");
    obj[key] = arg[1];
  });
  return obj;
  }

 function foo(x=5, y=7, z='foo') {}

// converting the function into a string
var fn = foo.toString();

// magic regex to extract the arguments 
var args = /\(\s*([^)]+?)\s*\)/.exec(fn);

//getting the object
var argMap = serialize(args[1]); //  {x: "5", y: "7", z: "'foo'"}

argument extraction method was taken from here: Regular Expression to get parameter list from function definition

cheers!

PS. as you can see, it casts integers into strings, which can be annoying at times. just make sure you know the input type beforehand or make sure it won't matter.

like image 1
silicakes Avatar answered Oct 16 '22 14:10

silicakes