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.
But what about custom Javascript objects? What would you say - which the two is better?
But what about custom Javascript objects? What would you say - which the two is better?
that like asking "what's better; a car or a chainsaw?"...
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'])
Javascript Guru Douglas Crockford says, you should prefer the dot-notation. The other notation requires a little more work on the js-engines as there is always a new string to handle with myObject["something"].
So, there is only a need for the myObject["something"]-notation if you are going some dynamic way like something = "somewhere"; myObject[something];
["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
Bookmarks