m2

xpath の結果を配列で返す

(function(){
function x(p,c) {
  return (function(e){return new Array(e.snapshotLength).join(',').split(',').map(function(a,b){return e.snapshotItem(b)})})(document.evaluate(p,c||document,null,7,null))
}
alert(x('/\/a'));
})()

.join(',').split(',') が泣ける感じ。

  • -

うわわ、0 が 1 になちゃうよ。

(function(){
  alert(new Array(0).join(',').split(',').length)
})()
  • -

これでどうだ。

(function(){
function x(p,c) {
  return (c = (function(e){return new Array(e.snapshotLength).join(',').split(',').map(function(a,b){return e.snapshotItem(b)})})(document.evaluate(p,c||document,null,7,null)))[0] ? c : [];
}
alert(x('/\/a[img]').length);
})()
    • -

(追記) みんなは $X を使いましょう。(Google ではなかなか検索できないので Bing りました。)

  • -

フィボナッチとか

function fibseq(n){
  return Array(n+1).toString().split(',').map(function(_,i,t){return t[i]=(t[i-1]||i)+(t[i-2]||0)});
}
alert(fibseq(25).pop());

for ループの方が地球に優しいです。