JavaScript using toString on a Function object to read text content

Posted by mseeley on Stack Overflow See other posts from Stack Overflow or by mseeley
Published on 2010-04-16T04:22:21Z Indexed on 2010/04/16 4:33 UTC
Read the original article Hit count: 214

Filed under:
|
|

Calling toString() on the function below returns different strings across browsers. I understand this is because ECMA-262 15.3.4.2 leaves wiggle room for each vendor.

Chrome returns the comments in addition to all syntax. Sadly Firefox 3.6 omits the comments. Based on Firefox's behavior I haven't tested IE, Opera, or Safari.

function foo() {
    /* comment */
    var bar = true;
}

Specifically, I am attempting to embed meta data within a specially formatted comment block within a function. Later the return value of the functions toString() method would be parsed and values returned as an object.

I've been unable to locate compatibility tables or alternatives to toString(). Does the community have any ideas? Btw, pre-processing JS files isn't an option. :(

Thanks a lot. :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about function