Subj : Re: Spidermonkey JS question To : netscape.public.mozilla.jseng,comp.lang.javascript From : =?ISO-8859-1?Q?Georg_Maa=DF?= Date : Wed Nov 05 2003 09:59 pm I?igo Koch wrote: > Hi, > > I need to create an object that at the same time has a string value: > > foo = "string1"; > foo.bar = "string2"; > > It must be possible since browsers have a lot of these cases: > location = "str"; > location.href = "str"; > etc.. Only the first sample contains an implicite cast; the second does not. The second sample uses a setter as written by Brendan. The first sample first assigns a string value to the variable foo. foo now contains a string value, not an object. foo.bar = "string2"; now casts foo temporary to a String wrapper object. This is caused by the element lookup operator. The string value "string2" is assigned to the bar property of the temporary string object. If you output foo you get "string1"; if you ouptut foo.bar, you get undefined or nothing. alert(foo + '\n' + foo.bar); this alerts: string1 undefined js> print(foo + '\n' + foo.bar); this writes: string1 -- Georg Maaß - bioshop.de D-76227 Karlsruhe, Westmarkstraße 82 HTML, XML / JavaScript, C++, Java, PHP, VB / CGI, JSP, ASP, ASP.net - The ultimative DHTML engine: http://gml-modul.sourceforge.net - http://sourceforge.net/projects/gml-modul .