[Javascript] Literal array declarations

Philip Thompson philthathril at gmail.com
Thu Oct 29 15:31:33 CDT 2009


On Oct 29, 2009, at 2:25 PM, Scott Reynen wrote:

> On Oct 29, 2009, at 1:01 PM, Philip Thompson wrote:
>
>> I ran across http://www.jslint.com/ and had it evaluate my code. One
>> of the things it suggested was to "Use the array literal notation  
>> []."
>> So, I had:
>>
>> var anArray = new Array();
>>
>> and JSLint wants me to do:
>>
>> var anArray = [];
>>
>> Well, I'm curious... why is the literal declaration preferred?
>
> It's not preferred by everyone, but it is preferred by Douglas
> Crockford, the author of JSLint, and here's a quote from one of his
> books on why he prefers it:
>
> http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful/383423#383423
>
>> If you forget to include the new prefix when calling a constructor
>> function, then this will not be bound to the new object. Sadly, this
>> will be bound to the global object, so instead of augmenting your
>> new object, you will be clobbering global variables. That is really
>> bad. There is no compile warning, and there is no runtime warning
>
>
> Peace,
> Scott

Thanks for your response. So basically it comes down to... he's afraid  
of being lazy or non-precise. "Hey, it won't work properly if I don't  
program it correctly." Yeah, big shock there.

Okay. Thank you.
~Philip



More information about the Javascript mailing list