(new Function(s)).toString() : browser comparison

H

HopfZ

If string variable s contains the following code, (new
Function(s)).toString() will return its normalized code in Firefox.
---------
var s /*..*/= 'abc'
var t = 1 //..
s = t+++t
if(s) return; else { var u = "abc" ; var r = /a/g}
---------


String variable s2 contains the following code. Only difference between
s and s2 is at the last line where a semicolon is omitted. (new
Function(s2)).toString() should result in SyntaxError.
---------
var s /*..*/= 'abc'
var t = 1 //..
s = t+++t
if(s) return; else { var u = "abc" var r = /a/g}
---------

Using the following test code, I tested Firefox, IE and Opera : the
return value of (new Function(s)).toString() and content of SyntaxError
from s2.
---------
<textarea id="inn_multi" cols="75" rows="10"></textarea><br>
<textarea id="inn_multi2" cols="75" rows="10"></textarea><br>
<script type="text/javascript" charset="utf-8">
function repr(o){ls = ''; for(i in o){ls+=i+' || '+o+'\n'} return
ls;}

var s = 'var s /*..*/= \'abc\'\n var t = 1 //..\ns = t+++t\nif(s)
return; else { var u = "abc" ; var r = /a/g}';
var m = document.getElementById('inn_multi');
try{ m.value = (new Function(s)).toString() } catch(e){ m.value =
'error\n'+repr(e)}

var s2 = 'var s /*..*/= \'abc\'\n var t = 1 //..\ns =
t+++t\nif(s) return; else { var u = "abc" var r = /a/g}'; //inc.
var m = document.getElementById('inn_multi2');
try{ m.value = (new Function(s2)).toString() } catch(e){ m.value =
'error\n'+repr(e)}
</script>
---------


Result:


Firefox 2
-------------
function anonymous() {
var s = "abc";
var t = 1;
s = t++ + t;
if (s) {
return;
} else {
var u = "abc";
var r = /a/g;
}
}
-------------
error
message || missing ; before statement
fileName || http://127.0.0.1:8001/bookmarkletgen.html
lineNumber || 34
stack || anonymous()@:0
Function("var s /*..*/= 'abc'\n var t = 1 //..\ns = t+++t\nif(s)
return; else { var u = \"abc\" var r = /a/g}")@:0
@http://127.0.0.1:8001/bookmarkletgen.html:31

name || SyntaxError
-------------


Opera 9
-------------
function ()
{
var s = "abc";
var t = 1;
s = t++ + t;
if (s)
return ;
else
{
var u = "abc";
var r = /a/g;
}
}
-------------
error
message || Statement on line 10: Parse error in function body: var s
/*..*/= 'abc'
var t = 1 //..
s = t+++t
if(s) return; else { var u = "abc" var r = /a/g}
Backtrace:
Line 10 of inline#2 script in
http://127.0.0.1:8001/bookmarkletgen.html
m.value = new Function(s2).toString();
m.value = "error\n" + repr(e);

opera#sourceloc || 10
-------------


IE 7
-------------
function anonymous() {
var s /*..*/= 'abc'
var t = 1 //..
s = t+++t
if(s) return; else { var u = "abc" ; var r = /a/g}
}
-------------
error
name || SyntaxError
message || Expected ';'
number || -2146827284
description || Expected ';'
-------------

I did this test because I wanted to know how (new
Function(s)).toString() can be used to normalize javascript code. It
turns out a javascript function to compress javascript code can be
written for Firefox and Opera without constructing full javascript
parser. But for cross-browser function, either make a full javascript
parser or restricts input only to semicolon-respecting code (as in
jsmin).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top