JavaScript:TDG tip: evaling String objects
I’m just getting around to reading my complimentary copy of David Flanagan’s excellent JavaScript: The Definitive Guide 5th Edition.
The first section of the book is the most in-depth description of the JavaScript language in the universe. And (having read the 4th edition roughly three times thru) I’ve already noticed the 5th edition contains several interesting new tidbits sprinkled throughout. Here’s something I never knew before…
Flanagan describes the subtle differences between primitive JavaScript datatypes (specifically strings, little ess) and their full-blown, often-elusive, Object counterparts (specifically Strings, big ess).
Here’s a juicy morsel for the delectation of the reader… while eval‘ing a primitive string has the expected result of interpreting the JavaScript program the string contains…
Rhino 1.6 release 5 2006 11 18
js> var s = "1+1";
js> typeof s
string
js> eval(s);
2
eval‘ing a String object has quite a different effect:
Rhino 1.6 release 5 2006 11 18
js> var S = new String("1+1");
js> typeof S
object
js> eval(S);
1+1
Utterly frustrating fascinating, no?
Calling the toString method on any object being passed to he eval function is probably a good habit to get into… jic.
About this entry
You’re currently reading “JavaScript:TDG tip: evaling String objects,” an entry on Todd Ditchendorf’s Blog.
- Published:
- 04.07.07 / 1pm
- Category:
- JavaScript/DHTML, Rhino
No comments
Jump to comment form | comments rss [?] | trackback uri [?]