friends, following what i learned in this post i'm busy rewriting my object.
i have a specific question. in my old code i would have something like: var datascape = new Object(); datascape.ini = function(){ datascape.availableWidth = $(window).width(); } }; can i rewrite it like this: var datascape= { 'ini': function(){ this.availableWidth = $(window).width(); } So that, if i need it somewhere else, i can just get to its value "datascape.availableWidth" or do i have to explicitate it ? var datascape= { 'ini': function(){ datascape.availableWidth = $(window).width(); } Thanks !!! On Wed, Dec 31, 2008 at 5:16 PM, pete higgins <phigg...@gmail.com> wrote: > >> and, yeah, I usually put in bare keys (sans quotes) unless necessary, too. >> Not sure why. I guess I just like the clean look. > > Yah, they just seem to be wasted bytes, huh? > > One thing to note, and the only reason I try to force myself to use > the quotes is for portability. If the data is "really" JSON and > expects to be consumed by languages not javascript, the quotes are > [likely] necessary, eg PHP. js and php can share a common json > fragment if they are quoted. > > $d = json_decode(file("someFile.json")); > foreach($d as $item => $val){ .. } > > really its just a distinction between "what js can do" and "JSON" > > this isn't JSON: > var foo = { bar:function(){ .. }, baz: new Date() }; > > (I know you know, too, btw) > > About anything can be a key in JS. (DomNodes can't, though btw, but > functions objects etc) > > var bar = { a:"b", c:"d" }; > var bar2 = [1,2,3,4]; > var foo = {}; > foo[bar] = "baz"; > foo[bar2] = "baz2"; > > if(bar in foo){ console.log(foo[bar]); } // baz > if(bar2 in foo){ console.log(foo[bar2]); } // baz2 > > Fun. Thanks for the banter, Karl. > > Regards, > Peter Higgins >