js小白模拟系列_模拟数组 fill,find,findIndex

"Hello World, Hello Blog"

Posted by wudimingwo on December 15, 2018

fill

1
2
3
4
5
            Array.prototype.myFill = function (value,start = 0,end = this.length) {
            	for(let i = start; i < end; i++) {
            	  this[i] = value;
            	}
            }

这里有个发现, es6默认值可以调用 this,确实方便


find

1
2
3
4
5
6
7
            Array.prototype.myFind = function (fn, start = 0,end = this.length) {
            	for(let i = start; i < end; i++){
            	  if(fn.call(this,this[i],i,this)){
            	    return this[i]
            	  }
            	}
            }

findIndex

1
2
3
4
5
6
7
8
            Array.prototype.myFindIndex = function (fn, start = 0,end = this.length) {
            	for(let i = start; i < end; i++){
            	  if(fn.call(this,this[i],i,this)){
            	    return i
            	  }
            	}
            	return -1
            }