Javascript disadvantages of "single var pattern"

In the JS Design Staps Stefanov book of templates, he writes: “you use one var statement and declare several variables separated by commas”, and then give an example of the “one var” template as follows:

function func() {
    var a = 1,
        b = 2,
        sum = a + b,
        myobject = {},
        i,
        j;

Stefanov additionally writes:

  • "It's good practice to also initialize a variable with an initial value at the time of its declaration."
  • "You can also do some actual work during the declaration, as is the case with sum = a + b in the previous code."

Now I have the code as follows, declaring the same number of variables with a single var template, but doing a bit more "actual work during declaration":

var html = '{purchaseQty}<br>FR:&nbsp; {fromLoc}'
    ,tpl = new Ext.XTemplate(html)
    ,srcReqLoc = record.get('SRC_REQUEST_LOC').trim()
    ,srcSupLoc = record.get('SRC_SUP_LOC').trim()
    ,fromLoc = srcReqLoc ? srcReqLoc : srcSupLoc
    ,tplCfg = {
        purchaseQty: purchaseQty
        ,fromLoc: fromLoc
    };

" "? BTW Javascript single var pattern. ?, , , .

, , , , trim() , record.get, undefined, " undefined " ( ;) . - - ?

+5
3

, . .

, . , , , , , - , . .

- .

, .., .

: , , var , .

, , , , - , , - :

var html       = '{purchaseQty}<br>FR:&nbsp; {fromLoc}'
    ,tpl       = new Ext.XTemplate(html)

    ,srcReqLoc = record.get('SRC_REQUEST_LOC').trim()
    ,srcSupLoc = record.get('SRC_SUP_LOC').trim()    
    ,fromLoc   = srcReqLoc ? srcReqLoc : srcSupLoc

    ,tplCfg    = {
        purchaseQty: purchaseQty
        ,fromLoc: fromLoc
    };

( = , .)

+4

( , ), vars , JavaScript .

JSLint:

, . JavaScript , . , var .

, - C C.

, Crockford , ( ).

+5

, , es6, , .

var , , , - - ' t , const .

0

All Articles