Re: FAQ Topic - How do I access a property of an object using a string? (2009-10-12)



In comp.lang.javascript message <rbadneCKEOpA4EvXnZ2dnUVZ8oGdnZ2d@gigane
ws.com>, Thu, 15 Oct 2009 03:41:48, Stefan Weiss <krewecherl@xxxxxxxxx>
posted:
On 14/10/09 20:37, Dr J R Stockton wrote:
The quoted example is foolish, because there can be no reasonable need
to use square brackets to express 'body'. It answers the Subject line
taken literally; but it does not correspond with the sort of case that
the putative original questioner must have had in mind.

An alternative example :
GMT = true // or false, depending
D = new Date()
St = "get" + (GMT ? "UTC" : "")
T = D[St + "Hours"]() + ":" + D[St + "Minutes"]()

That's far too complex for illustrating a simple point. The original
example is not very useful in itself, but it's simple enough to get the
message across.

I also think that examples in the FAQ should follow best practices
regarding code style. You don't declare your variables, you don't end
your statements with semicolons, you use very terse variable names which
can only be understood by reading through the complete context, and you
start non-constructor identifiers with a capital letter. It's all valid
JS, and it's fine if it suits your style preferences, but it's hardly a
schoolbook example. Just my €0.02.

That sort of thing can be left to the editor, or to later in the
discussion; consistency is most easily achieved by a single person.
Here, I am only concerned with getting the idea over to those who are
interested in altering the FAQ contents.

For myself, I see no need to distinguish constructor identifiers, though
I know it's fashionable. There are few native constructors, and a coder
should know their names ; and in the code I read there are very few non-
native constructors. I prefer to use an initial capital to distinguish
non-native identifiers from native ones and standard language words such
as "while", in JavaScript as in other languages. After all, a variable
identifier is grammatically a Proper Noun, and at least one natural
language uses capital letters for (IIRC) all nouns (e.g.
<http://de.wikipedia.org/wiki/Roskosmos>).

While variables should be declared, they will not necessarily be
declared nearby (it would be different if this were Pascal/Delphi, where
declarations carry type information which may be complex, and so should
be seen). And the code only makes practical sense if GMT is a variable
which varies.

I'm fine with the original, and I don't think it's necessary to change
it, but if we want something closer to what people are actually looking
for, how about

var index = 3;
myForm.elements["field" + index].focus();

or

var index = 3;
var images = document.getElementsByTagName("img");
images["icon" + index].src = "someImage.png";

From what I've seen here, images and form fields are the elements that
newcomers most often want to access by a composite key.

Agreed; and an example could well be adapted to using images of pre-
loaded digits.

Indeed, there could be a question along the lines of "How do I display a
countdown with fancy digits?" and the answer would include image access.
It may not be commonly asked; but read the code of various sites
associated with NASA and you'll see that it is a question which ought to
have been asked (I don't recall seeing corresponding ESA/RKA pages).

Any comment that you might have on English in
<URL:http://www.merlyn.demon.co.uk/zel-1882.htm>
and its siblings 1883, 1885, 1886 should be valuable.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
.



Relevant Pages

  • Re: Overcoming Transact-SQL ORDER BY limitations
    ... declare @s nvarchar ... I could use a calculated field and sort on ... >> SortOrder ... >> In my table, JobNo is a bigint, and the other fields are string fields. ...
    (microsoft.public.sqlserver.programming)
  • Re: Two things that bug me about C#
    ... If you want this sort of thing it can be had in VB.NET via its "modules" ... > 1) you can't declare anything outside of a class, enum, etc. ... > declare globals right after the namespace declaration. ... and thus waste time and space. ...
    (microsoft.public.dotnet.languages.csharp)
  • we shiver the systematic coverage
    ... We declare them, then we sort of whisper Walt and Edith's maximum shot. ...
    (sci.crypt)
  • Re: Is cast operator unary or binary? How many operands?
    ... using implicit int AND implicit extern. ... distinguishable from other identifiers by context. ... declare f as function taking one argument of type int, ... If one attempts to use a conventional scanner and parser generator ...
    (comp.lang.c)
  • Re: Auto Referencing in Word Documnets
    ... I've spent enough time cleaning up this sort of ... I am certainly not a public servant and I hope you are paying your taxes ... identifiers. ...
    (microsoft.public.word.docmanagement)