function addParams(baseFun, fn) { //工厂方法
var slice = Array.prototype.slice,
baseFunArgsCount = baseFun.length,
superFun = function() {
var oldArgs = slice.call(arguments, 0, baseFunArgsCount),
moreArgs = slice.call(arguments, baseFunArgsCount),
ret = baseFun.apply(this, oldArgs);
if (moreArgs.length > 0) {
ret = fn.call(this, superFun, ret, moreArgs);
}
return ret;
};
return superFun;
}
function getMore(fn, ret, args) { //辅助方法
return [ret].concat(fn.apply(this, args));
}
function $el(id) { //基础方法
return document.getElementById(id);
}
//通过ID取元素们
window._$el = addParams($el, getMore);
/*
<div id="doc">
<div id="hd"></div>
<div id="bd"></div>
<div id="ft"></div>
</div>
_$el('doc') //<div id="doc">…</div>
_$el('hd', 'ft') //[<div id="hd">…</div>, <div id="ft">…</div>]
_$el('doc', 'hd', 'ft') //[<div id="doc">…</div>, <div id="hd">…</div>, <div id="ft">…</div>]
*/