简体   繁体   English

从Array.prototype方法返回数组的值

[英]Return the value of array from Array.prototype method

I have the following code. 我有以下代码。

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

It's functional in that it modifies the array how I intend it to, but there's no return value. 它的功能在于它修改了我想要的数组,但没有返回值。

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

Will output [0,1,2], but 将输出[0,1,2],但是

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

Gives me undefined. 给我一个不确定的。 Can someone explain to me why "return this;" 有人可以向我解释为什么“归还这个”; in a prototyped method doesn't actually return the object? 在prototyped方法中实际上不返回对象?

Thanks. 谢谢。

您需要从初始呼叫返回。

return this.range(start + 1, count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM