Prev: FAQ Topic - How can I access the client-side filesystem? (2010-05-11)
Next: apparently wrong function.
From: Matt Kruse on 12 May 2010 14:23 On May 12, 1:00 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Third, with either solution the issue remains that a property name may not > contain the delimiter then, when that would not be necessary had a different > approach been chosen. IOW, it is not possible to access > obj["property"][".property."]["[prop|erty]"] > with > $prop(obj, 'property|.property.|[prop|erty]', '|') > because that would access > obj["property"][".property."]["[prop"]["erty]"] Are you being intentionally obtuse? $prop(obj, 'property/.property./[prop|erty]', '/') or $prop(obj, 'property#.property.#[prop|erty]', '#') or $prop(obj, 'propertySHUTUPTHOMAS.property.SHUTUPTHOMAS[prop|erty]', 'SHUTUPTHOMAS') Take your pick. I agree this is not the best syntax. I proposed it as a solution only to counter your nit-picking about dots in property names. In reality, if the properties contained such characters, I would either not use the function or I would modify it, or perhaps make an alternative version that takes arguments as parameters. Matt Kruse
From: Thomas 'PointedEars' Lahn on 12 May 2010 14:50 Matt Kruse wrote: > Thomas 'PointedEars' Lahn wrote: >> Third, with either solution the issue remains that a property name may >> not contain the delimiter then, when that would not be necessary had a >> different approach been chosen. IOW, it is not possible to access >> obj["property"][".property."]["[prop|erty]"] >> with >> $prop(obj, 'property|.property.|[prop|erty]', '|') >> because that would access >> obj["property"][".property."]["[prop"]["erty]"] > > Are you being intentionally obtuse? No, you are. The issue is fairly obvious, but you choose to ignore it. > $prop(obj, 'property/.property./[prop|erty]', '/') > or > $prop(obj, 'property#.property.#[prop|erty]', '#') > or So property names containing `/' and `#' could not be accessed then, i.e. neither $prop(obj, 'property/.property./[prop#/erty]', '/') nor $prop(obj, 'property#.property.#[prop#/erty]', '#') could access e.g. obj["property#"][".prop/erty."]["#[prop#/erty]"] aso. And if you used an array of delimiters instead of only one delimiter, or a regular expression, the matched characters could not be contained in a property name. > $prop(obj, 'propertySHUTUPTHOMAS.property.SHUTUPTHOMAS[prop|erty]', > 'SHUTUPTHOMAS') Shut up yourself and power up your brain for a change. > Take your pick. I will continue to choose the rational, logical course of action; so not yours. > I agree this is not the best syntax. You are a master of understatement. Your $prop() will be unable to access a property if any property name along the way contains any designated delimiter. That way you are limiting its capabilities. Needlessly. Score adjusted PointedEars -- Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
From: Stefan Weiss on 12 May 2010 14:52 On 11/05/10 15:44, 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"); > or > deep( myJsonObject, > "locations.ca.sandiego.directory.people.smith.john.phone"); > > This would return undefined if any element along the chain is not > there, or index is out of bounds, etc. If the chain gets to the end, > it would return the last-evaluated value. This would be convenient for > accessing deep properties of objects that may or may not exist, and > not having to manually check the chain in your code. This may be obvious, but did you consider simply catching and ignoring the ReferenceError? try { var color = body.firstChild.style.backgroundColor; } catch(e) {} Maybe it doesn't look as nice as your deep() call, but at least there's no custom syntax involved, and you can use arbitrary property names, including all the counter examples mentioned by Thomas, and even expressions like childNodes[i+1]. -- stefan
From: Scott Sauyet on 12 May 2010 15:05 Thomas 'PointedEars' Lahn wrote: > Scott Sauyet wrote: >> Since the presumed use is something like >> var xyz = $prop(document, "forms[0].foo[].value"); >> or if necessary, >> var xyz = $prop(obj, 'property|.property.|[property]', '|'); >> and not >> var xyz = $prop(obj, myStringVar); >> it's easy enough to see by inspection that the delimiter is not >> included in the string. > > Utter nonsense. > > First of all, to $prop() there is exactly no difference with regard to how > the value was passed. That's not only JavaScript 101, it's Programming 101. That's correct. And similarly, an API like document.getElementById(value) does not prohibit you from supplying a number or the window object as the value parameter. But those are still not good ideas. To me an API like this is something more like a macro than a function call. Of course someone could misuse it. But that's not enough of a reason for discarding it. The only reason I would have for using it would be to make my code simpler. I would never use it with anything other than a string literal. If the language had a facility to prevent the user from passing something else, I would take advantage of it; as it stands, its limitations would have to be documented. Perhaps it was later in Programming 101 that students learn that not everything you might want to do is supplied by the language or the rest of the programming environment, that you might have to extend that environment with your own code, and that sometimes that code has to make compromises between simplicity and completeness. > [ ... ] > But all of this is completely unnecessary if you let the programming > language provide the delimiter, i.e. arguments, Arrays, or a combination > thereof. It's not that I disagree. But I think your arguments are overstated. If Matt is going to publish this and promote it as an amazing tool that will drastically increase the productivity of any web developer, then he really should skip the string parsing and go with the more complete API. But if he is going to use this on a few projects with a half-dozen other coders, I would suggest that simplicity should rule. -- Scott
From: Matt Kruse on 12 May 2010 15:37
On May 12, 1:50 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > So property names containing `/' and `#' could not be accessed then, i.e. > neither > $prop(obj, 'property/.property./[prop#/erty]', '/') > nor > $prop(obj, 'property#.property.#[prop#/erty]', '#') > could access e.g. > obj["property#"][".prop/erty."]["#[prop#/erty]"] Are you missing the basic point that you PICK the delimiter based on the string you are passing in? 1. Pick your property string to evaluate 2. If the properties contain . or [ or ], then pick a delimiter 3. Choose the delimiter to be a character or sequence of characters that does not exist in your property string Is that so hard to understand? Similar to perl, where you can do: $var =~ s/a/b/; or $var =~ s|a/b|c/d|; or $var =~ s#a/b#c/d#; Nevertheless, your examples are sufficiently insane to not warrant consideration in any real case that I will ever expect to encounter. Matt Kruse |