quick javascript properties query

Soldato
Joined
6 Mar 2008
Posts
10,085
Location
Stoke area
hi,

Just working through some tutorials on Javascript and it's use in HTML5 and we've just started on objects (which I think are just arrays of data).

However, it's talking about properties of objects and shows two examples

fido.weight - which is 40
fido["breed"] - which is mixed

it makes out that you should always use the ["string"] method when the property is a string and the . method when it's a integer but doesn't really explain why.

I've tried the . method with both and it works fine.

Is there a reason to use them both or is it industry standard to just use the . method? :)

noob question I know but I don't like bad habits.
 
Everything in JavaScript is an object, even arrays :) and you can use either, but of course if the key of your object is a reserved word or contains invalid characters the dot notation won't work.

I've no idea about performance but JS God "Douglus Crockford" has always suggested that you should use dot notation of square bracket notation (also known as subscript) as it is shorter to write and slightly simpler to read.

Hope that helps :)
 
it makes out that you should always use the ["string"] method when the property is a string and the . method when it's a integer but doesn't really explain why.

I've tried the . method with both and it works fine.

Is there a reason to use them both or is it industry standard to just use the . method? :)

noob question I know but I don't like bad habits.

I completely disagree with the book you are reading. The dot notation is faster and I think it's much clearer to read (as it's a very common notation) and using the dot notation where possible is recommended by one of the most respected JavaScript books (The good parts - which I strongly recommend you read). From what I've seen, it is industry standard to use the dot notation wherever possible.

The square brackets notation is primarily required for property names which are unknown at run time. e.g. myObject["string" + x]

edit: you say you don't like bad habits so PLEASE read "The good parts": http://www.amazon.co.uk/JavaScript-...7742/ref=sr_1_1?ie=UTF8&qid=1324465510&sr=8-1
 
Last edited:
I completely agree with what AGD said. Dot notation always, every day, unless you don't know the specific parameter you want at run time, then use array notation.
 
I completely disagree with the book you are reading. The dot notation is faster and I think it's much clearer to read (as it's a very common notation) and using the dot notation where possible is recommended by one of the most respected JavaScript books (The good parts - which I strongly recommend you read). From what I've seen, it is industry standard to use the dot notation wherever possible.

The square brackets notation is primarily required for property names which are unknown at run time. e.g. myObject["string" + x]

edit: you say you don't like bad habits so PLEASE read "The good parts": http://www.amazon.co.uk/JavaScript-...7742/ref=sr_1_1?ie=UTF8&qid=1324465510&sr=8-1

The book you reference is by the same author the other guy was mentioning :confused:

I prefer dot notation as well, unless the object is an array where I use square brackets as that is similar to a lot of other languages... this is my personal preference and not based on any form of research!
 
The dot notation is faster

Pretty sure that's not true, last time I check all browser but Safari 4 performed the same, but it is worth mentioning that use of a variable within a square bracket notation IS slower.

Code:
var obj = {'foo': 'bar'};

// Fastest
obj['foo'];

// Slower, due to extra Prototype type lookup
var foo = 'foo';
obj[string];

Also note you can also access values via their numerical keys also

Code:
var string = 'foobar';
string[0]; // returns f
string[3]; // returns b

Wouldn't personally use the above, but you can do it :) just proof that everything is an object in JS.
 
Back
Top Bottom