Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do I optimize this (node.js)?

Name: Anonymous 2012-05-25 16:51

The loop seems unnecessary but I don't know how to get rid of it.


var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = "";
for(var i = 0; i < a.length; i++){
    var d = typeof a[i] === 'function' ? a[i]() : a[i];
    c = c.concat(d);
}
console.log(c);


var a is generated once at runtime from an array of strings and another array of functions. The functions obviously return dynamic content so doing something like this var a = a[0]+a[1]()+a[2]; by loop once at runtime is not an option since I'd be saving the result of the function and not the function itself.

I've seen some code where a tailor-made function function(){return a[0]+a[1]()+a[2];} is created at runtime that returns the strings and functions() correctly but I don't know how to achieve something like that. In the example I saw the JavaScript source stored in a "string" and editted via String methods and then eval() evaluated. I don't even know if it's worth it in that case.

Thoughts? Help?!

Name: Anonymous 2012-05-25 20:12

For each function that is created, create a run string that contains the name and the data inside.

' a = [ "begin", function() { return "middle"}, "end ]; a();'

Join all of those run strings together, then do an eval on the result. May want to add a = null at the end to keep the functions from piling up or...something.

How's that?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List