[Javascript] Literal array declarations

Scott Reynen scott at randomchaos.com
Thu Oct 29 14:25:35 CDT 2009


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




More information about the Javascript mailing list