Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

toSource() failed ?

2 views
Skip to first unread message

franck.fr

unread,
Feb 14, 2006, 11:25:56 AM2/14/06
to
Hello,
I noticed that toSource() result is incomplete in some cases :

var a = function() { return arguments.callee.b }
a.b = 'foo';
a(); // returns 'foo'
a.toSource(); // returns '(function () {return arguments.callee.b;})'

b is a part of a and cannot be found in the result of toSource().

( tested with firefox 1.5.0.1 )

Franck.

Brendan Eich

unread,
Feb 14, 2006, 4:34:08 PM2/14/06
to franck.fr

Yes, toSource (uneval is the global function to use on any value to
produce a string that, when eval'ed, results in an isomorphic or equal
value) is not perfect. There is no syntax, in particular, for a lambda
with expandos added. Perhaps something like

((__tmp = function () { return arguments.callee.b; }).b = "foo", __tmp)

but JS has no such expression-scoped variables. This is fodder for deep
thinking on better syntax and more uniform notation, for Edition 4 or a
later ECMA iteration (we can experiment in advance in SpiderMonkey, so
don't take these words as discouraging).

/be

Mike Shaver

unread,
Feb 14, 2006, 5:29:06 PM2/14/06
to Brendan Eich, dev-tech-...@lists.mozilla.org
On 2/14/06, Brendan Eich <bre...@meer.net> wrote:
> with expandos added. Perhaps something like
>
> ((__tmp = function () { return arguments.callee.b; }).b = "foo", __tmp)
>
> but JS has no such expression-scoped variables.

((function () { function a() { return arguments.callee.b; } a.b="foo";
return a; })())

?

Mike

Brendan Eich

unread,
Feb 16, 2006, 1:08:12 AM2/16/06
to Mike Shaver, dev-tech-...@lists.mozilla.org

As I pointed out to shaver, although lambdas are what pass for blocks or
let-expressions in JS, they also create a second function object, which
is costly enough that I wouldn't use them here.

It seems to me toSource/uneval is misconceived. What most people want
is not round-tripping, hi-fi serialization, but a programmer friendly
string representation (Pythonic repr). Not all bits must be included,
and the string returned need not eval without error.

Of course, SpiderMonkey is committed to compatibility, so we won't be
changing toSource except to fix bugs and elaborate compatibly (that is,
eval-ably). So suggestions for more universal result syntax are still
welcome.

Comments on repr desirability in JS (MochiKit rolls its own as it must)
also welcome.

/be

Brendan Eich

unread,
Feb 16, 2006, 1:08:12 AM2/16/06
to Mike Shaver, dev-tech-...@lists.mozilla.org

As I pointed out to shaver, although lambdas are what pass for blocks or

Message has been deleted
0 new messages