Results 1 to 8 of 8

Thread: JSLint new Array() ? (Solved)

  1. #1
    Sencha User michaelc's Avatar
    Join Date
    Dec 2007
    Location
    Bay Area - CA
    Posts
    505

    Default JSLint new Array() ? (Solved)

    So after a small issue with Minification, I have had JSLint turned on all the time.

    it gives me a warning with this statement
    var x = new Array(); Use Array literal notation []

    This is correct, how does it want me to create an array ?
    Trying to be a good netizen but I don't know what it wants.

  2. #2

    Default

    Code:
    var x = [];
    Wes

  3. #3
    Sencha User michaelc's Avatar
    Join Date
    Dec 2007
    Location
    Bay Area - CA
    Posts
    505

    Default

    Ok . . . what is the rationalization for that one ?

  4. #4

    Default

    It's easier to read, write, maintain, and initialize than new Array().
    It skips using the Array function which __could__ be overridden...
    Douglas Crockford, who wrote JSLint, prefers to eliminate the new keyword wherever possible. It eliminates semantic errors caused when someone uses new keyword when they didn't need it. JSLint will also complain any time new proceeds a word that starts with a lower case letter, so again, eliminating unnecessary new keywords benefits JSLint.

    [] is effectively the same as new Array(). The only difference is the intricate, bewildering formal params used by the Array constructor.
    Wes

  5. #5
    Sencha User michaelc's Avatar
    Join Date
    Dec 2007
    Location
    Bay Area - CA
    Posts
    505

    Default

    Ok, got it thanks

  6. #6
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854

    Default

    The same reasoning also applies to the use of new Object(). Better to do
    Code:
    var x = {};

  7. #7
    Sencha Premium Member steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,675

    Default

    these hints are given by jsLint direct. What i don't like is the warning for each non strict comparison, but there are cases where strict is not possible. Anyway you can turn off this warning.

    btw - i strongly recommend using rocketstarapps plugin for zend/eclipse which has jslint built in.
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  8. #8
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    Wanrings for non strict comparison? You mean the 'x == 0' warning?

    Most people read that warning wrong.

    It doesn't mean that you HAVE to write
    Code:
    if (x === 0) {
    instead of
    Code:
    if (x == 0) {
    You could just as well write:
    Code:
    if (!x) {

Similar Threads

  1. Comma convention JSlint
    By emredagli in forum Community Discussion
    Replies: 7
    Last Post: 14 Oct 2009, 5:52 AM
  2. JSLint in Visual Studio
    By lepipele in forum Community Discussion
    Replies: 5
    Last Post: 13 Nov 2008, 7:29 AM
  3. Array.sortDates ,Array.unique, and extended Array.indexOf
    By wm003 in forum Ext 2.x: User Extensions and Plugins
    Replies: 3
    Last Post: 4 May 2008, 12:38 AM
  4. JSLint AIR Edition
    By pmarcotte in forum Community Discussion
    Replies: 1
    Last Post: 14 Sep 2007, 2:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •