[RESOLVED] Difference between object['element'] and object.element?
Hi all, long time no see! How are you doing?
Anyway, here's my question: Is there any difference between object['element'] and object.element?
Thanks a dozen!
They reference the same element, so the only difference is symantics.
You can use a string, a var containing a string, or an integer within the braces to access an element.
However, the other syntax is literal, and cannot be a var.
Some may also make the comment that this is the "old" way of referencing things, but it still works.
Thanks for your quick reply
Which of the two is the 'old' way?
The "new" way would involve DOM methods.
Actually, I am not talking about referencing DOM objects, but about defining arrays and defining custom objects (for object oriented scripts). I know how to use DOM methods for DOM HTML objects.
that like asking "what's better; a car or a chainsaw?"...
Originally Posted by pilau
for hand-coded source, ob.prop is quicker and more readable.
for machine-generated code, op["prop"] syntax allows spaces, reserved names like ob['class'], var-refs ( ob[stringVar] ), and expressions (ob['pr'+'op'])
Well consider this, in this case I didn't really know the difference between the car and the chainsaw...
Originally Posted by rnd me
Another question - if I declare an object using one way can I call it's elements using the other?
Also, is one way prefered for custom arrays defined through the Array class and the other for objects defined through custom classes?
So, there is only a need for the myObject["something"]-notation if you are going some dynamic way like something = "somewhere"; myObject[something];
Greetings from Germany,
www.youngvisions.net | www.deutscherfussballblog.de | www.schneider.ws
["xxx"] is called array syntax, but it works just as well for objects.
i don't think there's any speed penatly for using ["xxx"] vs .xxx, the scriptBdy is aready parsed by the time ['xxx'] or .xxx would be assessed. most benchmarks i've seen fail to report any diff, and when they do, it's less than %1...
there is no short-hand of what goes where; you'll have to develop the judgment to decide that for yourself on a case-by-case basis.
will non-special names and hand-coded routines, i would use .xxx first; it's more readable.
Thanks a lot guys
Now I understand this a lot better - on the whole, there isn't any important difference between the two. I was always using the dot method, anyway
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)