views:

33

answers:

1
+2  Q: 

javascript syntax

   var ret = [] 
             ,xresult = document.evaluate(exp, rootEl, null,
                         XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)
             ,result = xresult.iterateNext();
   while (result) {
     ret[ret.length]= result;
     result = xresult.iterateNext();
   }

can anyone explain me what is the ret = [],..,... syntax? Initializing array?

+2  A: 

You're right. This code:

var ret = [] 
             ,xresult = document.evaluate(exp, rootEl, null,
                         XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)
             ,result = xresult.iterateNext();

Could be rewritten as:

var ret = [];
var xresult = document.evaluate(exp, rootEl, null,
                         XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
var result = xresult.iterateNext();

var foo = []; initializes foo as an empty array.

artlung
@artlung: yea but I don't see connection between empty `ret`, `xresult` and `result`... please elaborate more on this if you can. unless there is no connection and just short hand writing - one line initializing.
Michael
Right, there's no connection. So for example var `var a = 1, b = 2, c = 3;` is one way to write it, but I could do it like this too: `var a = 1; var b = 2; var c = 3;` ... using commas is merely coding style. Based on the code you posted, there's no link between `ret`, `xresult` and `result`.
artlung
Well, I shouldn't say "no link" -- the `while` loop your posted uses those three variables to populate the `ret` array. But the `var` line is just initialization.
artlung