12

Is it safe to rely on Function.prototype.toString to return a string that will parse as a valid javascript function (for user-defined functions)?

Are there any commonly-used javascript engines that deviate from the norm as far as how they represent function objects in string form?

I have seen this question, but I'm not sure if it's asking the same thing. I don't care if the formatting is exactly the same in all implementations or whatever, I'm more worried about some minified js engine just stripping out the whole function body...

Another related question, but not closely related enough to have a satisfying answer for this question.

Community
  • 1
  • 1
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • 2
    See http://perfectionkills.com/those-tricky-functions/ and http://perfectionkills.com/state-of-function-decompilation-in-javascript/ – Bergi Sep 22 '14 at 10:31

2 Answers2

3

I think it's safe since it's a standard. Every serious engine would do. That's also what my project Jscex is based on. It works for all the browsers (even the legacy IE6) and Node.js. I do this kind of things for year. :)

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
  • Sounds scexy. I am exploring iframe sandboxes :) I'm mostly worried about phones, embedded, weird IE security modes, stuff like that. Just want to look at some tests or studies or live use-cases that are not prototype.js for reassurance i guess. – Dagg Nabbit Dec 10 '11 at 05:06
  • Well, I've tested [this (one of Jscex's samples)](http://files.zhaojie.me/jscex/samples/async/sorting-animations.html?quick) in Windows Phone, iOS, Android phones, they all works. – Jeffrey Zhao Dec 10 '11 at 05:09
  • Jscex looks pretty interesting. Is there any reason you're using eval instead of the Function constructor? I think the Function constructor is supposed to be significantly faster. That's very cool though. +1 for Jscex ;) – Dagg Nabbit Dec 10 '11 at 05:22
  • `eval` is easy to use and keep everything in current context (scope, closure). and each function would only be applied **once** when the script loads. Also, `eval` is a dev tools, which would gone in production after AOT. – Jeffrey Zhao Dec 10 '11 at 05:27
  • i see, so the production site would have pre-compiled code instead of the eval'd code on the test site. I will have to look into Jscex more, it sounds like it could be a big time saver – Dagg Nabbit Dec 10 '11 at 05:29
  • Thanks, you can contact me via the email if you meet any problem. The doc in github is a little bit old (but most of the things and the big pictures are correct) since I'm focusing on local community and writing Chinese docs & articles. The samples are always work so they're good references. – Jeffrey Zhao Dec 10 '11 at 05:46
1

It should be noted that eval'd code will take on the current scope and the Function constructor will only take on the global scope.

function createClosure() {
    var x = 20,
        y = 10;

    // Doesn't know what x & y are
    var fn = new Function("return x + y");

    // Evaluates normally
    var result = eval("x + y");
}
  • I suppose that's worth noting, but I was really trying to figure out if all implementations supported a Function#toString that could be rehydrated. Turns out at least some versions of Opera Mobile don't support it. – Dagg Nabbit Jun 01 '14 at 20:20