Prev: FAQ Topic - How can I access the client-side filesystem? (2010-05-11)
Next: apparently wrong function.
From: Asen Bozhilov on 11 May 2010 15:45 Matt Kruse wrote: > Does anyone here use a general convenience method for deep property > access that will not throw an error if any property along the chain is > undefined? > > For example: > > deep(document, "body.firstChild.style.backgroundColor"); > or > deep("myElementId.childNodes[3].id"); Square bracket notation allow to used returned result by expression, and that result is not bound as Identifiers rules defined by ECMA-262. With your strategy you would have problems. For example: var obj = {}; obj['.prop.'] = true; obj['[prop]'] = true; print(obj['.prop.']); print(obj['[prop]']) Your strategy is too complex and implementation will be terrible any time. For example you can use evil for your purposes: eval(properties); Of course that is not solve the design problems. I will prefer to use array which contain each property name. For example: deep(context, ['property1', 'property2', 'propertyN']);
From: Matt Kruse on 11 May 2010 16:59 On May 11, 1:06 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > > True. But since you're passing in a string, couldn't you just do: > > $prop("myObj.prop1.prop2."+current.value) > > ? > Sure, unless current is undefined, which I think is the original > problem we're trying to solve. :-) I think it's good to solve the most general case that is reasonable and useful, but not EVERY general case. > And of course we could want > $props("myObj.prop1.prop2[current.deeply.nested.value]") > as well. Sure, you COULD do that, but I don't imagine I ever WOULD. So solving that case holds little value for me. But using my function, I suppose you could do: if ( $prop("current.deeply.nested.value") ) { $prop("myObj.prop1.prop2."+$prop()) } Matt Kruse
From: Matt Kruse on 11 May 2010 17:03 On May 11, 2:45 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > Square bracket notation allow to used returned result by expression, > and that result is not bound as Identifiers rules defined by ECMA-262. > With your strategy you would have problems. For example: > var obj = {}; > obj['.prop.'] = true; > obj['[prop]'] = true; > > print(obj['.prop.']); > print(obj['[prop]']) > > Your strategy is too complex But why would you EVER do that? I'm not building this for idiots! > and implementation will be terrible any > time. I don't know, my implementation is pretty straight-forward and works well. > For example you can use evil for your purposes: > eval(properties); Which will still throw errors. > I will prefer to use > array which contain each property name. For example: > deep(context, ['property1', 'property2', 'propertyN']); How is that any better than joining the array with "." and passing a single string?! Matt Kruse
From: Garrett Smith on 11 May 2010 17:07 Asen Bozhilov wrote: > Matt Kruse wrote: > [...] > eval(properties); > > Of course that is not solve the design problems. I will prefer to use > array which contain each property name. For example: > > deep(context, ['property1', 'property2', 'propertyN']); > Does deep check own or prototype? -- Garrett comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on 11 May 2010 17:10
Matt Kruse wrote: > Asen Bozhilov wrote: >> Square bracket notation allow to used returned result by expression, >> and that result is not bound as Identifiers rules defined by ECMA-262. >> With your strategy you would have problems. For example: >> var obj = {}; >> obj['.prop.'] = true; >> obj['[prop]'] = true; >> >> print(obj['.prop.']); >> print(obj['[prop]']) >> >> Your strategy is too complex > > But why would you EVER do that? It can be necessary for mapping values that property names contain dots or brackets. > I'm not building this for idiots! ISTM you are not building this for people who do DOM scripting either. <form>...<input name="foo[]">...</form> > [...] >> I will prefer to use array which contain each property name. For example: >> deep(context, ['property1', 'property2', 'propertyN']); > > How is that any better than joining the array with "." and passing a > single string?! The property names are clear. PointedEars -- var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16 |