开发者

JSLint -Tolerate inefficient subscripting?

开发者 https://www.devze.com 2022-12-23 03:30 出处:网络
I\'m reading the JSlint Options Documentation to understand each of the available options, and have come across one that I开发者_如何学JAVA don\'t quite understand, and can\'t find any useful informat

I'm reading the JSlint Options Documentation to understand each of the available options, and have come across one that I开发者_如何学JAVA 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


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.)


See here. Looks like subscript notation is doing:

document.forms['myformname'];

instead of

document.forms.myformname;
0

精彩评论

暂无评论...
验证码 换一张
取 消