7

I have a JavaScript object that looks something like this:

{ bacon: [Function], hello: [Function], tables: [Function] }

Where [Function] is an actual JavaScript function.

I want to write this to a .js file with contents like:

var Templates = /*source code here*/

How can I get the source code for the object and function properties as a string, such that eval'ing this "source code string" will give me back the same object?

7
  • 3
    I don't think I understood your requirement Commented Apr 3, 2013 at 2:33
  • 3
    Have you tried calling toString on the function? For an object you could just use a JSON parser. Commented Apr 3, 2013 at 2:33
  • .toString() also gives back "[object Object]" Commented Apr 3, 2013 at 2:36
  • .toString() seems to work on the functions actually, but not on the object. Commented Apr 3, 2013 at 2:37
  • 1
    @ChrisHayes: Why doesn't it have meaning? Let me rephrase then: I want a string such that calling eval() on it will yield exactly the same object. JSON cannot represent functions. Commented Apr 3, 2013 at 2:41

5 Answers 5

5

I rolled my own serializer:

var templates = { /* object to stringify */ };
var properties = [];
_.each(templates, function(value, key) {
    properties.push(JSON.stringify(key)+': '+value.toString());
});
var sourceCode = 'var Templates = {' + properties.join(",\n") +'};';

This gives me back:

var Templates = {"bacon": function anonymous(locals, attrs, escape, rethrow, merge) { ... },
"hello": function anonymous(locals, attrs, escape, rethrow, merge) { ... },
"tables": function anonymous(locals, attrs, escape, rethrow, merge) { ... }
};

(snipped bodies for brevity)

Sign up to request clarification or add additional context in comments.

10 Comments

I was actually writing something like this up. Glad you figured it out though.
This should work, but don't use _ if the question wasn't tagged as such.
Eval'ing the toString representation of a function is not going to work all the time. Closure state will be lost for one thing. There may be other issues too?
@Bart: I'm OP... I have access to underscore.
@hifier: Yes....there are definitely issues. If you look at the output I posted, that's definitely not the full source of the function, because when I execute those functions they do something different. I'm trying to figure that out now.
|
2

In Javascript a function is an object. The Function object supports the method toString(). This will actually give you the source code of a function. Like this:

function foo() {
    var a = 1 + 1;
}

alert(foo.toString()); // will give you the above definition

Comments

1

If I understand what you're trying to say, this would show me the function code:

myObj = {

    myMethod: function() {
        console.log("This is my function");
    }
}

console.log(myObj.myMethod.toString());

6 Comments

I need the entirety of myObj, not just one property.
@Mark You could run a loop of some kind to get the source for all of them.
Curly braces, and property names included. I guess I could write that part of the string by hand, but I don't quite understand why I can get the source for a function but not the object part.
That's probably because an object can have a reference to itself. You can't serialize recursive objects.
@Bart: That's a good point. In this scenario, however, I don't, but that would explain why this may not be implemented.
|
0

You can use JSON.stringify to accomplish this via the replacer argument:

var myObj = { a  : 1, b : function(val) { doStuff(); };

var replacer = function(key, val) {
      return "key : " + val.toString();
};

console.log(JSON.stringify(myObj, replacer));

I haven't tested this but the idea should be sound.

1 Comment

Running that verbatim (aside from a missing curly brace), I get back "key : [object Object]". It still can't serialize the object.
0

If you may want to use node.js, you can use a lib that can handle ASTs. You may have a look at https://github.com/substack/node-falafel

1 Comment

I'm really not sure how this is helpful. It looks like it lets you manipulate the AST once you have the source code. I was having trouble getting the source code in the first place, and I don't need to manipulate after I do have it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.