What are some real world uses for function.toString()?

Well, you can use it to easily redefine a function:

function x() { alert('asdf'); }
eval(x.toString().replace('asdf','hello'));
x();

This will alert the string "hello" instead of the string "asdf".

This may be useful. On the other hand, self modifying code is often frowned upon because of the difficulty to maintain it...


This is an old question, but here are my 2 cents.

Using node.js, this becomes useful for creating functions on the serverside which are then embedded into a page and sent to the client.

For example, the dot.js template engine works by first compiling templates into a function, which can then be executed to generate the HTML code.

eg:

var compiled = dot.compile("<h1>{{=it.header}}</h1>");
var html_output = compiled({header: "hello world"});

So, if we want to make use of templates in the client without each client having to compile them first, we can serve them a page containing the evaluated result of:

"var compiled = " + dot.compile("<h1>{{=it.header}}</h1>").toString();

which will then provide a "compiled" function client-side, for use compiling data such as that sent from ajax requests into HTML client-side.


I've used it to automatically generate named-parameter versions of functions. For example, if you have a function

function f(a, b, c) {
    return a * b + c;
}

you can extract the parameter names from f.toString() and use it to generate a function that you can call like this:

namedParametersF({ a: 2, b: 3, c: 4}); // f(2, 3, 4);

Here's an implementation of this idea:

// Get an array of parameter names from a function
Function.parameters = function(f) {
    // Find the parameter list in f.toString()
    var m = /function[^\(]*\(([^\)]*)\)/.exec(f.toString());
    if (!m) {
        throw new TypeError("Invalid function in parameters");
    }

    var params = m[1].split(',');
    for (var i = 0; i < params.length; i++) {
        // trim possible spaces
        params[i] = params[i].replace(/^\s*|\s*$/g, '');
    }
    return params;
};

// Convert f to a function that accepts named parameters
Function.withNamedParameters = function(f) {
    var params = Function.parameters(f);
    return function(args) {
        var argsArray = new Array(params.length);
        for (var i = 0; i < params.length; i++) {
            argsArray[i] = args[params[i]];
        }
        return f.apply(this, argsArray);
    };
};

I have a slightly more flexible implementation of this that can go the other direction (Function.withPositionalParameters) on GitHub: http://gist.github.com/132782.

Tags:

Javascript