不知道为什么我对这个结果一直不确定,任何帮助都会很棒。结果假设为以 x 值开头的数组。谢谢
var tester = [1,2,4];
Array.prototype.cons = function(x){
function reduce(results,y){
if(y == 1){
results.unshift(x);
return results;
}
else{
results.push(this[y-1]);
y = y-1;
reduce(results, y);
}
}
return reduce([], this.length);
}
document.getElementById('test').innerHTML = tester.cons(0)