473,785 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ Topic - How do I access a property of an object using a string?

-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation. What you are looking
for is the square bracket notation in which the dot, and the
identifier to its right, are replaced with a set of square
brackets containing a string. The value of the string matches
the identifier. For example:-

//dot notation
var bodyElement = document.body;

//square bracket notation, using an expression
var bodyElement = document["bo"+"dy"];

http://www.jibbering.com/faq/faq_not..._brackets.html
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Nov 17 '06 #1
19 2635
FAQ server wrote:
-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation.
Is the word "equivalent " correct here? It seems like square bracket
notation is more powerful in general.

Nov 18 '06 #2
Peter Michaux said the following on 11/17/2006 7:37 PM:
FAQ server wrote:
>-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation.

Is the word "equivalent " correct here?
No, it is not correct.
It seems like square bracket notation is more powerful in general.
It is, but, not for the reasons most people think.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 18 '06 #3

Randy Webb wrote:
Peter Michaux said the following on 11/17/2006 7:37 PM:
FAQ server wrote:
-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation.
It seems like square bracket notation is more powerful in general.

It is, but, not for the reasons most people think.
What do most people think? What are the real reasons?

Nov 18 '06 #4
Peter Michaux said the following on 11/17/2006 8:28 PM:
Randy Webb wrote:
>Peter Michaux said the following on 11/17/2006 7:37 PM:
>>FAQ server wrote:
-----------------------------------------------------------------------
FAQ Topic - How do I access a property of an object using a string?
-----------------------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation.
It seems like square bracket notation is more powerful in general.
It is, but, not for the reasons most people think.

What do most people think?
From my personal experience, when the issue gets discussed it is always
brought up about being "ECMA compatible" or more "cross browser
compatible". When it was last talked about, I asked for a browser that
would take bracket notation and not dot notation (with the exception
that is noted in the FAQ) and nobody - to date - has come up with one
where document.forms['myForm'].elements['myInput'].value will work when
document.myForm .myInput.value won't.
What are the real reasons?
The reason bracket notation is more powerful than dot notation? One very
easy example:

PHP generated forms with names of "myElement[]". PHP creates an array of
them on the server. You can't access that element with Dot Notation in JS.

There is nowhere that dot notation will work that bracket notation
won't. There are times though that dot notation won't work but bracket
notation will. That alone makes it more powerful.

The counter argument is that dot notation is faster - and it is.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 18 '06 #5
Peter Michaux wrote:
FAQ server wrote:
>---------------------------------------------------------
FAQ Topic - How do I access a property of an object using
a string?
---------------------------------------------------------

There are two equivalent ways to access properties: the dot
notation and the square bracket notation.

Is the word "equivalent " correct here? It seems like square
bracket notation is more powerful in general.
Bracket notation is often erroneously perceived as a special syntax for
accessing Array objects (it is even asserted as being such numerous poor
javascript books and web 'resources'). This leads to situations where
people create Array objects and then dynamically add exclusively
non-array index properties to those arrays (employing the object-ness of
the Array and not its Array-ness).

Dot notation and bracket notation are both property accessors, they both
employ precisely the same algorithm (once the expression in the brackets
of a bracket notation property accessor has been evaluated and
type-converted to a string (if necessary)) to do precisely the same job
(reference the properties of objects). In that sense they are
equivalent.

The only sense in which bracket notation is "more powerful" is that it
has no restrictions on the character sequence used as a property name,
while dot notation can only be constructed from Identifiers and so the
syntax rules for Identifiers apples to the character sequences of the
property names that may be accessed with dot notation. This leaves
bracket nation more widely applicable (as, for example, the only option
when referencing the 'array index' properties of an Array) and more
flexible as the value of expression used in the brackets is determined
at run-time rather than when the code is written.

So: All dot notation property accessors may be substituted with bracket
notation property accessors and the only things that make the reverse
not possible are that not all string values could qualify as valid
Identifiers and that the string values used for the property names are
determined at runtime in a bracket notation property accessor.

It is better for the FAQ to stress the often under appreciated
equivalence of the two types of object property accessors and leave it
to readers of the referenced article to understand the wider
possibilities that bracket notation introduces.

Richard.

Nov 18 '06 #6
In article <ej************ *******@news.de mon.co.uk>, Richard Cornford
<Ri*****@litote s.demon.co.ukwr ites

<snip>
>The only sense in which bracket notation is "more powerful" is that it
has no restrictions on the character sequence used as a property name,
while dot notation can only be constructed from Identifiers and so the
syntax rules for Identifiers apples to the character sequences of the
property names that may be accessed with dot notation. This leaves
bracket nation more widely applicable (as, for example, the only option
when referencing the 'array index' properties of an Array) and more
flexible as the value of expression used in the brackets is determined
at run-time rather than when the code is written.
<snip>

You've given us the right description there. Bracket notation is "more
widely applicable" and "more flexible".

"More powerful" is misleading or meaningless, or both. For instance,
it's easier to spot a typing error in a.l than in a.["l"], so dot
notation is sometimes more "powerful".

John
--
John Harris
Nov 18 '06 #7
In message <45************ ***********@new s.sunsite.dk>, Sat, 18 Nov 2006
00:00:01, FAQ server <ja********@dot internet.bewrit es
>
There are two equivalent ways to access properties: the dot notation
and the square bracket notation. What you are looking for is the square
bracket notation in which the dot, and the identifier to its right, are
replaced with a set of square brackets containing a string. The value
of the string matches the identifier. For example:-

//dot notation
var bodyElement = document.body;

//square bracket notation, using an expression
var bodyElement = document["bo"+"dy"];

http://www.jibbering.com/faq/faq_not..._brackets.html


Properties can be addressed by dot notation and by square bracket
notation.

Dot notation is appropriate where the "term" is known at programming
time.

Square bracket notation is necessary if the "term" must be supplied at
run time.

For example:-

// dot notation
var bodyElement = document.body;

// square bracket notation, using a computed variable
var MyStringVal = "bo"+"dy"
var bodyElement = document[MyStringVal];

http://www.jibbering.com/faq/faq_not..._brackets.html

-

IMHO, that wording allows for cases, if any exist, where "term" is known
at programming time but bracket notation is necessary.

In the case of exec(control.va lue) programming time is nested in
run time.

-

So why did someone respected here advise me to use the first of these
without mentioning the second?

document.forms["F"].X0.value
document.forms. F.X0.value

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Nov 18 '06 #8
Dr J R Stockton said the following on 11/18/2006 3:44 PM:

<snip>
Square bracket notation is necessary if the "term" must be supplied at
run time.
That is not always true. Square bracket notation is necessary if the
"term" has characters in that JS doesn't like. PHP's array-ness of
inputs comes to mind first. If the name of the inputs is "myInput[]"
then you have to use bracket notation to access it using it's name but
the "term" will be known at programming time.

That very specific issue is dealt with in Section 4.25.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 19 '06 #9
In message <GO************ ********@telcov e.net>, Sat, 18 Nov 2006
19:46:57, Randy Webb <Hi************ @aol.comwrites
>Dr J R Stockton said the following on 11/18/2006 3:44 PM:

<snip>
>Square bracket notation is necessary if the "term" must be supplied at
run time.

That is not always true. Square bracket notation is necessary if the
"term" has characters in that JS doesn't like. PHP's array-ness of
inputs comes to mind first. If the name of the inputs is "myInput[]"
then you have to use bracket notation to access it using it's name but
the "term" will be known at programming time.

That very specific issue is dealt with in Section 4.25.
When I wrote 'Square bracket notation is necessary if the "term" must be
supplied at run time', I meant 'Square bracket notation is necessary if
the "term" must be supplied at run time'.

If I had meant 'Square bracket notation is necessary if and only if the
"term" must be supplied at run time', I would have written 'Square
bracket notation is necessary if and only if the "term" must be supplied
at run time'.

The example you refer to is covered by the explanatory wording further
down : 'IMHO, that wording allows for cases, if any exist, where "term"
is known at programming time but bracket notation is necessary.'.

One could add, to the bit you quoted above, 'or if the "term" has
characters in that JS doesn't like' and any further cases; but one must
avoid the appearance of giving a complete list if the list is not
necessarily complete.

IMHO, the cases I gave are sufficient for the FAQ, though more might be
added in Notes.

AISB, ISTM that in the USA "Logic 101" states that "A implies B" implies
"B implies A". In fact, "A implies B" implies "not B implies not A".
FAQ 4.25 is OK; it says that illegal characters require bracket
notation, and does not say that bracket notation is only required for
illegal characters.
<FAQENTRY>
The first, index section of the FAQ misuses <H4& <H5- and, when
viewed without CSS, <H5is by default rather small. It would be better
done with nested <ul>, perhaps all in a <divthat makes it Bold.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.c om/faq/ Old RC FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4753
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
7
8869
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
11
6601
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
8
3995
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE( Pk_myPrimaryKey INTEGER CONSTRAINT pk PRIMARY KEY DESCRIPTION 'This is the primary key of the table',
8
376
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I detect Opera/Netscape/IE? ----------------------------------------------------------------------- The « navigator » object contains strings which specify the browser and version; however, this is in general not very genuine. Mozilla (and therefore Netscape 6+) allows this to be freely set, and Opera and IE allow it to be modified. There are...
7
402
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string? ----------------------------------------------------------------------- There are two ways to access properties: the dot notation and the square bracket notation. What you are looking for is the square bracket notation in which the dot, and the identifier to its right, are replaced with a set of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.