1

我有以下代码。

Array.prototype.range = function(start, count) {
  this.push(start);
  if(this.length == count){
     return this;
  };
  this.range(start+1, count)
}

它的功能在于它修改了我想要的数组,但没有返回值。

test = new Array;
test.range(0,3);
console.log(test);

将输出 [0,1,2],但是

test = new Array;
console.log(test.range(0,3));

给我未定义。有人可以向我解释为什么“退货”吗?在原型方法中实际上并不返回对象?

谢谢。

4

1 回答 1

4

You need to return from the initial call.

return this.range(start + 1, count);
于 2013-10-11T19:20:42.303 回答