15

I've been linting some of my code and got some errors back saying it is better to use dot notation. I found out I was using square bracket notation (with clarity from this great post), however, I wanted to ask why exactly does Crockford prefer dot notation? The project I'm working on has used SBN for it's entirity, and I don't think it's confusing or un-readable, but if there are distinct reasons to user dot, I will correct it.

Just want to understand it fully before proceeding!

Community
  • 1
  • 1
streetlight
  • 5,968
  • 13
  • 62
  • 101

4 Answers4

15

As I best understand Crockford, I think it comes down to consistency and avoiding the use of reserved words. On his site, he states:

The dot notation can be used when the subscript is a string constant in the form of a legal identifier. Because of an error in the language definition, reserved words cannot be used in the dot notation, but they can be used in the subscript notation.

Since you can refer to reserved words in subscript notation, it could cause confusion. Basically, avoid using reserved words as the names of your object's members. The dot notation enforces this (through the language -- an error as Crockford calls it), and so it would be considered a better coding practice to avoid using reserved words.

Also on the same site, he also states that the dot notation is "a little more convenient".

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • how do you workaround when using google closure compiler. Example: var Promise = window['Promise']; – sbr Oct 07 '14 at 22:13
  • But isn't the subscript notation more versatile? You can use variable to invoke a member using the subscript notation, but using dot notation it invokes that variable as if it is a member. i.e.: `var obj = {name: 'Java Script'}; var _key = 'name'; obj['_key'] === 'name'; //=> true; obj._key === 'name'; //=> false` – benjaminz Mar 31 '16 at 18:01
9

From "JavaScript: The Good Parts" it says on page 21:

The . notation is preferred because it is more compact and it reads better.

The book is written by Douglas Crockford himself.

Lasse Christiansen
  • 10,205
  • 7
  • 50
  • 79
3

There is no problem with either syntax, and both will work in all environments. However, by using dot notation where possible, you can save three characters every time.

Source

It is important to remember that JSLint standards are based solely on opinion. Most are usually popular opinion, but some are debatable. That being said, I prefer JSHint instead which allows you to turn off whatever options you don't agree with.

jbabey
  • 45,965
  • 12
  • 71
  • 94
2

I believe it is to differentiate array elements from object properties and have less confusing code.

For ex:

var x = [];
x.push(stuff);

here x[0] is how we access it.

var y = { foo: "bar" };

y['foo'] as well as y.foo work, but to make it clear that foo is a property of the object y and not an element in the array y, we can use dot notation for objects.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • it's the same operator used to index an array. Ie an array with one element has a property called "0". So indexing into an array is a matter of using `[]` to access a property of an object – Rune FS Nov 07 '12 at 13:58
  • Never knew that! How do we use it with arrays? – techfoobar Nov 07 '12 at 13:59
  • @Christoph I rephrased becuase yes it was incorrect because you can't start an identifier with a number, my point was there's nothing special with the ´[]` operator for an array it's the same operator for all other objects and arrays in JS are not really arrays they are a specific class of objects the number in [] is not an index but a name of a property – Rune FS Nov 07 '12 at 14:06
  • @RuneFS - Arrays are nothing but objects yes, but *that theoretical reality* is irrelevant when all you are trying to do is write readable code. When it comes to readable code, there **is** a difference between `company.name` and `companies[0]`. The built-in `Array` object is there for a reason as it does make certain scenarios easier to implement. – techfoobar Nov 07 '12 at 14:14
  • @techfoobar yes they access different properties :) – Rune FS Nov 08 '12 at 05:49