简体   繁体   中英

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

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?

Thanks.

您需要从初始呼叫返回。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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