不確定爲什麼我一直未定義這個結果,任何幫助都會很好。結果是假設開始時x值爲數組。感謝值保持返回爲undefined
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)
檢查'this'值在'reduce'功能...它不是你所期望不亮*此*行其不能再封閉 – ManseUK
什麼'results.push(this [y-1]);' – ManseUK