On Feb 10, 10:05 am, Richard Quadling <rquadl...@googlemail.com>
wrote:
[...]
> So, it seems the only place where things go wonky is with eval().

`eval ` treats passed string as a Program, and evaluates it as such.
There's nothing wonky about it : )

>
> And the odd behaviour indicates that the toString() method isn't
> called on a string object when using eval(). More wierd.
>
> >>> s1 = "2 + 2"
> "2 + 2"

String literal (primitive) is created.

> >>> s2 = new String("3 + 3")
> 3 + 3

String object (with the internal [[Value]] set to "3 + 3") is created.

> >>> eval(s1)
> 4

"2 + 2" string primitive is evaluated as a Program and the result of
its production is returned - `4`

> >>> eval(s2)
> 3 + 3

`eval` does *NOT* do anything when you pass it a non-string value
(which you do in this case - as `s2` is an Object, not a string). It
simply returns what was given (which is an object with a value of "3 +
3")

> >>> eval(s2.toString())
>
> 6

Here, you take that String object and call its `toString` method.
`toString` returns whatever is stored in String object's internal
[[Value]] - that is "3 + 3". The returned "3 + 3" is a string
primitive and so `eval` rightfully evaluates it, returning `6`

Read ECMA specs <URL http://bclary.com/2004/11/07/ > if you really
want to know what's going on under the hood (or even to be able to
explain/understand simple examples like the above). You'll be
surprised how clear everything becomes once you do ; )

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to