Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Forked from isaacs/comma-first-var.js
Last active June 15, 2017 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/26262fe2e54cef341d94041a8fabd716 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/26262fe2e54cef341d94041a8fabd716 to your computer and use it in GitHub Desktop.
js-comma-in-deepth: A better coding convention for lists and object literals in JavaScript

js-comma-in-deepth

    
// standard style
var a = "ape",
    b = "bat",
    c = "cat",
    d = "dog",
    e = "elf",
    f = "fly",
    g = "gnu",
    h = "hat",
    i = "ibu";

// comma-first style
var a = "ape"
    , b = "bat"
    , c = "cat"
    , d = "dog"
    , e = "elf"
    , f = "fly"
    , g = "gnu"
    , h = "hat"
    , i = "ibu"
    ;

Objects:

    
// JSON.stringify style

var o = {
        a : "ape",
        b : "bat",
        c : "cat",
        d : "dog",
        e : "elf",
        f : "fly",
        g : "gnu",
        h : "hat",
        i : "ibu"
    },
    a = [
        [ "ape", "bat" ],
        [ "cat", "dog" ],
        [ "elf", "fly" ],
        [ "gnu", "hat" ],
        [ "ibu" ]
    ];

  

// comma-first
var o ={ 
    a : "ape"
    , b : "bat"
    , c : "cat"
    , d : "dog"
    , e : "elf"
    , f : "fly"
    , g : "gnu"
    , h : "hat"
    , i : "ibu"
    }
    , a = [
        [ "ape", "bat" ]
        , [ "cat", "dog" ]
        , [ "elf", "fly" ]
        , [ "gnu", "hat" ]
        , [ "ibu" ]
    ];

Addendum: effects on the return statement.

    
// It does not break.

return [ 
    1
    , 2
    , 3
];
// returns [1,2,3]

return { 
    a : "ape"
    , b : "bat"
};
// returns {a:"ape",b:"bat"}
@xgqfrms-GitHub
Copy link
Author

addendum & 附录

//附录:对 return语句的影响。 
//它不会破坏



return [ 
    1
    , 2
    , 3
];
// returns [1,2,3]

return { 
    a : "ape"
    , b : "bat"
};
// returns {a:"ape",b:"bat"}
```

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 15, 2017

ES6

// standard style
let a = "ape",
    b = "bat",
    c = "cat",
    d = "dog",
    e = "elf",
    f = "fly",
    g = "gnu",
    h = "hat",
    i = "ibu";


// standard style
const x = {},
      y = [],
      z = () => {};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment