Mutation Person
Mutation Person

Reputation: 30498

JSLint -Tolerate inefficient subscripting?

I'm reading the JSlint Options Documentation to understand each of the available options, and have come across one that I don't quite understand, and can't find any useful information regarding it elsewhere.

sub - Tolerate inefficient subscripting

true if subscript notation may be used for expressions better expressed in dot notation.

Can anyone shed more light as to what this means?

Thanks

Upvotes: 4

Views: 1004

Answers (2)

bobince
bobince

Reputation: 536399

JavaScript object members can be accessed using dot or subscript (square bracket) notation:

o.foo
o['foo']

...are the same thing. Square bracket notation is necessary for accessing members whose names can't be used in dot notation:

o['hello!']

or for accessing members from a dynamic name:

var name= issomething? 'foo' : 'bar';
o[name]

But for simple o['foo'] you don't need it. Normally the o.foo form is easier to read so it's considered better practice to use that. Some programmers coming from other languages may prefer to use square brackets for objects being using ‘like a mapping’ rather than ‘like an object’, but it's all the same to JS.

(JSlint is claiming that square bracket form is also “less efficient”, but if so then the difference is minuscule and not really worth bothering about.)

Upvotes: 7

Tom
Tom

Reputation: 22841

See here. Looks like subscript notation is doing:

document.forms['myformname'];

instead of

document.forms.myformname;

Upvotes: 2

Related Questions