Joined April 2016
·

mdsy

here is another implementation:
function split2(str, separator, limit) {
if (limit==0) return [str];
var a = str.split(separator, limit);
if(a.length == limit) {
let s = a.join(separator) + separator;
a.push( str.substr(s.length) );
return a;
}else{
return [str];
}
}

this way you don't have to split the entire string (in case it is huge), also shouldn't the function always return an array?

Posted to Subclassing JavaScript's Array class over 1 year ago

hi...
first, why do you skip the first argument and assign it to an unused variable name ???
second, the resulting pseudo-array does not behave like an array, try this:

var Wrapper = function() {
var args = Array.prototype.slice.call(arguments, 0);
this.push.apply(this, args);
return this;
}
Wrapper.prototype = Object.create(Array.prototype);
Wrapper.prototype.first = function(){ return this[0] };
var a = new Wrapper('cool',1,2,3);
a.length = 6; //OK
;a.map(function(x) {return x*2}); //OK
a.push('last one'); //OK
;a.first(); //OK
a.length = 3;
a[6]; //it is still there!!!! WRONG, should be undefined
so you see, you still haven't subclassed a Javascript Array ;-)

Achievements
1 Karma
0 Total ProTip Views