Array.prototype.push
let arr = [1 ,2 ,3 ,4 ,5 ]
let arrLike = {0 :1 ,1 :2 ,length :2 }
let obj = {}
Array .prototype.myPush = function ( ) {
var length = this .length ? this .length : (this .length = 0 ) && 0
var index = 0
while (index < arguments .length) {
this [length] = arguments [index]
++index
++length
}
this .length = length
return this .length
}
arr.myPush(1 ,2 ,3 ,4 )
Array .prototype.myPush.call(obj,1 ,2 ,{})
Array .prototype.myPush.call(arrLike,3 ,4 ,{})
console .log(arr)
console .log(obj)
console .log(arrLike)
複製程式碼
Array.prototype.pop
Array .prototype.myPop = function ( ) {
var length = this .length ? this .length : (this .length = 0 ) && 0
if (length === 0 )return
var last = this [length-1 ]
delete this [length - 1 ]
--this .length
return last
}
console .log(Array .prototype.myPop.call(arr))
console .log(Array .prototype.myPop.call(arrLike))
console .log(arr,obj,arrLike)
複製程式碼
Array.prototype.shift
Array .prototype.myShift = function ( ) {
var length = this .length ? this .length : 0
if (length === 0 )return
var first = this [0 ]
var index = 1
while (index < this .length) {
this [index - 1 ] = this [index]
index ++
}
delete this [length - 1 ]
--this .length
return first
}
console .log(arr.myShift())
console .log(Array .prototype.myShift.call(obj))
console .log(Array .prototype.myShift.call(arrLike))
複製程式碼
Array.prototype.unshift
Array .prototype.myUnshift = function ( ) {
if (!this .length)return
var length = this .length
var arglength = arguments .length
this .length = length + arglength
var index = length - 1
while (index >= 0 ) {
this [index + arglength] = this [index]
--index
}
index = arglength - 1
while (index >= 0 ) {
this [index] = arguments [index]
--index
}
return this .length
}
console .log(arr.myUnshift(0 ,1 ,3 ,4 ,5 ))
console .log(Array .prototype.myUnshift.call(obj,0 ,1 ,3 ,4 ,5 ))
console .log(Array .prototype.myUnshift.call(arrLike,0 ,1 ,3 ,4 ,5 ))
複製程式碼
Array.prototype.slice
Array .prototype.mySlice = function (begin,end ) {
if (!this .length)return
var arr = []
var index = 0
begin = typeof begin === 'number' ?
begin < 0 ? this .length + begin
: begin
: 0
end = typeof end === 'number' ?
end > this .length ? this .length
: end
: this .length
while (begin < end) {
arr[index] = this [begin]
begin++
index++
}
return arr
}
console .log(arr.mySlice(-4 ))
console .log(Array .prototype.mySlice.call(obj,0 ,2 ))
console .log(Array .prototype.mySlice.call(arrLike,0 ,1 ))
複製程式碼
Array.prototype.concat
Array .prototype.myConcat = function ( ) {
if (!arguments .length)return
var arr = []
var index = 0
while (index<this .length) {
arr[index] = this [index]
++index
}
var i = 0
while (i < arguments .length) {
var el = arguments [i]
if (el instanceof Array ) {
if (el.length){
var j = 0
while (j < el.length) {
arr[index] = el[j]
++index
++j
}
}
}else {
arr[index] = el
++index
}
++i
}
return arr
}
console .log(arr.myConcat(1 ,[2 ,3 ],[4 ,[5 ,[6 ],[7 ]]]))
console .log(arr.concat(1 ,[2 ,3 ],[4 ,[5 ,[6 ],[7 ]]]))
console .log(arr)
複製程式碼
Array.prototype.splice
Array .prototype.mySplice = function (start,deleteCount ) {
if (!this .length)return
var arr = []
start = typeof start === 'number' ?
start > this .length ? this .length
: start < 0 ? this .length + start < 0 ? 0
: this .length + start
: start
: 0
deleteCount = typeof deleteCount === 'number' ?
deleteCount < 0 ? 0
: deleteCount > this .length - start ? this .length - start
: deleteCount : deleteCount === undefined ? this .length - start
: 0
var args = arguments .length > 2 ? Array .prototype.mySlice.call(arguments ,2 ) : []
var argLength = args.length
var oIndex = start
var moveLength = argLength - deleteCount
var delIndex = deleteCount + start
var addIndex = argLength + start
var index = 0
while (start < delIndex) {
arr[index] = this [start]
this [start] = null
++start
++index
}
if (moveLength > 0 ){
var i = this .length - 1
this .length += moveLength
while (i >= oIndex) {
this [i+moveLength] = this [i]
--i
}
}else {
var i = this .length
if (start < this .length){
while (start < i) {
this [start+moveLength] = this [start]
++start
}
}
this .length += moveLength
}
var i = 0
while (oIndex < addIndex) {
this [oIndex] = args[i]
++i
++oIndex
}
return arr
}
console .log(arrLike)
console .log(Array .prototype.mySplice.call(arrLike,1 ,1 ))
console .log(arrLike)
console .log(arr.mySplice())
console .log(arr)
複製程式碼
Array.prototype.reduce
Array .prototype.myReduce = function (callback ) {
var arr = this .mySlice()
var len = arr.length
var index = 0
var initialValue
if (arguments .length >= 2 ){
initialValue = arguments [1 ]
}else {
while (index < len && !(arr[index] in arr)) {
++index
}
if (index >= len) return
initialValue = arr[index++]
}
while (index < len) {
if (arr[index] in arr){
initialValue = callback.call(null , initialValue, arr[index], index, arr)
}
++index
}
return initialValue
}
var sum = [0 ,1 ,2 ,3 ,4 ].myReduce(function (accumulator, currentValue, currentIndex, array ) {
console .log(accumulator, currentValue, currentIndex, array)
return accumulator + currentValue;
});
var sum = [, 1 , ,3 ,,].myReduce(function (accumulator, currentValue, currentIndex, array ) {
console .log(accumulator, currentValue, currentIndex, array)
return accumulator + currentValue;
});
複製程式碼
Array.prototype.reduceRight
Array .prototype.myReduceRight = function (callback ) {
var arr = this .mySlice()
var len = arr.length-1
var index = len
var initialValue
if (arguments .length >= 2 ){
initialValue = arguments [1 ]
}else {
while (index >= 0 && !(arr[index] in arr)) {
--index
}
if (index <= 0 ) return
initialValue = arr[index--]
}
while (index >= 0 ) {
if (arr[index] in arr){
initialValue = callback.call(null , initialValue, arr[index], index, arr)
}
index--
}
return initialValue
}
var sum = [0 ,1 ,2 ,3 ,4 ].myReduceRight(function (accumulator, currentValue, currentIndex, array ) {
console .log(accumulator, currentValue, currentIndex, array)
return accumulator + currentValue;
},2 );
var sum = [, 1 , ,3 ,,].myReduceRight(function (accumulator, currentValue, currentIndex, array ) {
console .log(accumulator, currentValue, currentIndex, array)
return accumulator + currentValue;
},2 );
複製程式碼
Array.prototype.forEach
Array .prototype.myForEach = function (callback ) {
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
callback.call(context, this [index], index, this )
index++
}
}
[1 ,2 ,3 ,4 ,5 ].forEach(function (current, index, arr ) {
console .log(current, index, arr, this .a)
},{a :1 })
[1 ,2 ,3 ,4 ,5 ].myForEach(function (current, index, arr ) {
console .log(current, index, arr, this .a)
},{a :1 })
複製程式碼
Array.prototype.map
Array .prototype.myMap = function (callback ) {
var arr = []
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
arr.myPush(callback.call(context, this [index], index, this ))
index++
}
return arr
}
console .log([1 ,2 ,3 ,4 ,5 ].map(function (current, index, arr ) {
console .log(current, index, arr)
return index + this .a
},{a :1 }))
console .log([1 ,2 ,3 ,4 ,5 ].myMap(function (current, index, arr ) {
console .log(current, index, arr)
return index + this .a
},{a :1 }))
複製程式碼
Array.prototype.filter
Array .prototype.myFilter = function (callback ) {
var arr = []
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
var el = this [index]
callback.call(context, el, index, this ) && arr.myPush(el)
index++
}
return arr
}
console .log([1 ,2 ,3 ,4 ,5 ].filter(function (current, index, arr ) {
console .log(current, index, arr)
return index > this .a
},{a :1 }))
console .log([1 ,2 ,3 ,4 ,5 ].myFilter(function (current, index, arr ) {
console .log(current, index, arr)
return index > this .a
},{a :1 }))
Array .prototype.myFilter = function (callback ) {
var arr = []
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
var el = this [index]
callback.call(context, el, index, this ) && arr.myPush(el)
index++
}
return arr
}
console .log([1 ,2 ,3 ,4 ,5 ].filter(function (current, index, arr ) {
console .log(current, index, arr)
return index > this .a
},{a :1 }))
console .log([1 ,2 ,3 ,4 ,5 ].myFilter(function (current, index, arr ) {
console .log(current, index, arr)
return index > this .a
},{a :1 }))
複製程式碼
Array.prototype.every
Array .prototype.myEvery = function (callback ) {
var every = true
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
if (!callback.call(context, this [index], index, this )) {
every = false
break
}
index++
}
return every
}
console .log([1 ,2 ,3 ,4 ,5 ].every(function (current, index, arr ) {
console .log(current, index, arr)
return current > this .a
},{a :0 }))
console .log([1 ,2 ,3 ,4 ,5 ].myEvery(function (current, index, arr ) {
console .log(current, index, arr)
return current > this .a
},{a :0 }))
複製程式碼
Array.prototype.some
Array .prototype.mySome = function (callback ) {
var every = false
var len = this .length
var index = 0
var context = arguments [1 ] || this
while (index < len) {
if (callback.call(context, this [index], index, this )) {
every = true
break
}
index++
}
return every
}
console .log([1 ,2 ,3 ,4 ,5 ].some(function (current, index, arr ) {
console .log(current, index, arr)
return current > this .a
},{a :10 }))
console .log([1 ,2 ,3 ,4 ,5 ].mySome(function (current, index, arr ) {
console .log(current, index, arr)
return current > this .a
},{a :10 }))
複製程式碼
Array.prototype.find
Array .prototype.myFind = function (callback,context ) {
context = context || window
var len = this .length
var i = 0
while (i < len) {
if (callback.call(context,this [i],i,this ))
return this [i]
i++
}
return undefined
}
console .log([4 , 6 , 8 , 12 ].myFind(function (item ) {
return item + this .a > 10
},{a :5 }));
console .log([4 , 6 , 8 , 12 ].find(function (item ) {
return item + this .a > 10
},{a :5 }));
複製程式碼
Array.prototype.findIndex
Array .prototype.myFindIndex = function (callback,context ) {
context = context || window
var len = this .length
var i = 0
while (i < len) {
if (callback.call(context,this [i],i,this ))
return i
i++
}
return -1
}
console .log([4 , 6 , 8 , 12 ].myFindIndex(function (item ) {
return item + this .a > 10
},{a :5 }));
console .log([4 , 6 , 8 , 12 ].findIndex(function (item ) {
return item + this .a > 10
},{a :5 }));
複製程式碼
Array.prototype.join
Array .prototype.myJoin = function (separator ) {
var separator = typeof separator === 'string' ? separator : ','
var len = this .length
var str = ''
if (!len) return str
var index = 1
str = this [0 ] ? this [0 ].toString() : ''
while (index < len) {
str += separator + (this [index] ? this [index].toString() : '' )
index++
}
return str
}
console .log([1 ,null ,,{},[],/2 /].myJoin(',' ) === [1 ,null ,,{},[],/2 /].join(',' ))
複製程式碼
Array.prototype.reverse
Array .prototype.myReverse = function ( ) {
if (!this .length) return this
var len = this .length - 1
var index = 0
var mid = Math .floor(this .length / 2 )
while (index < mid) {
var lastIndex = len-index
var tem = this [index]
var last = this [lastIndex]
var indexEmpty = !(index in this )
var lastIndexEmpty = !(lastIndex in this )
if (lastIndexEmpty){
delete this [index]
}else {
this [index] = last
}
if (indexEmpty){
delete this [lastIndex]
}else {
this [len-index] = tem
}
index++
}
return this
}
var arr1 = [1 ,2 ,,3 ,,4 ,,5 ]
var arr2 = [1 ,2 ,,3 ,,4 ,,5 ]
arr1.myReverse()
console .log(arr1)
arr2.reverse()
console .log(arr2)
複製程式碼
Array.prototype.sort
function quickSort (arr,low,high,cb ) {
if (low<high){
var mid = partition(arr,low,high,cb)
quickSort(arr,low,mid-1 ,cb)
quickSort(arr,mid+1 ,high,cb)
}
return arr
}
function partition (arr,low,high,cb ) {
var poivt = arr[low]
while (low<high) {
while (low<high && cb(arr[high],poivt) >= 0 ) {
high--
}
arr[low] = arr[high]
while (low<high && cb(arr[low],poivt) <= 0 ) {
low++
}
arr[high] = arr[low]
}
arr[low] = poivt
return low
}
Array .prototype.mySort = function (cb ) {
return quickSort(this ,0 ,this .length-1 ,cb)
}
var arr1 = [3 ,5 ,5 ,-1 ,65 ,6 ,41 ,2 ,51 ,11 ,52 ,8 ]
var arr2 = [3 ,5 ,5 ,-1 ,65 ,6 ,41 ,2 ,51 ,11 ,52 ,8 ]
function fcb (a,b ) {
return a - b
}
console .log(arr1.mySort(fcb))
console .log(arr2.sort(fcb))
複製程式碼
Array.prototype.indexOf
Array .prototype.myIndexOf = function (search,fromIndex ) {
fromIndex = fromIndex ? typeof fromIndex === 'number' ? fromIndex
: typeof fromIndex === 'string' ? (fromIndex-=0 ) && fromIndex === fromIndex ? fromIndex
: 0 : 0 : 0
var index = -1
var len = this .length
var i = fromIndex < 0 ? len + fromIndex : fromIndex
while (i < len) {
if (search == this [i]){
index = i
break
}
i++
}
return index
}
console .log(arr1.myIndexOf(5 ,{}) == arr1.indexOf(5 ,{}))
console .log(arr1.myIndexOf(5 ,[]) == arr1.indexOf(5 ,[]))
console .log(arr1.myIndexOf(5 ,[1 ]) == arr1.indexOf(5 ,[1 ]))
console .log(arr1.myIndexOf(5 ,'1' ) == arr1.indexOf(5 ,'1' ))
console .log(arr1.myIndexOf(5 ,'1e' ) == arr1.indexOf(5 ,'1e' ))
console .log(arr1.myIndexOf(5 ,true ) == arr1.indexOf(5 ,true ))
console .log(arr1.myIndexOf(5 ,NaN ) == arr1.indexOf(5 ,NaN ))
console .log(arr1.myIndexOf(5 ,-1 ) == arr1.indexOf(5 ,-1 ))
console .log(arr1.myIndexOf(5 ,-5 ) == arr1.indexOf(5 ,-5 ))
複製程式碼
Array.prototype.lastIndexOf
Array .prototype.myLastIndexOf = function (search,fromIndex ) {
fromIndex = fromIndex ? typeof fromIndex === 'number' ? fromIndex
: (fromIndex-=0 ) && fromIndex === fromIndex ? fromIndex
: 0
: 0
var index = -1
var i = fromIndex < 0 ? fromIndex + this .length > 0 ? fromIndex + this .length : 0 : fromIndex > this .length ? this .length : fromIndex
while (i > 0 ) {
if (search == this [i]){
index = i
break
}
i--
}
return index
}
console .log(arr1.myLastIndexOf(5 ,{}) == arr1.lastIndexOf(5 ,{}))
console .log(arr1.myLastIndexOf(5 ,[]) == arr1.lastIndexOf(5 ,[]))
console .log(arr1.myLastIndexOf(5 ,[1 ]) == arr1.lastIndexOf(5 ,[1 ]))
console .log(arr1.myLastIndexOf(5 ,'1' ) == arr1.lastIndexOf(5 ,'1' ))
console .log(arr1.myLastIndexOf(5 ,'1e' ) == arr1.lastIndexOf(5 ,'1e' ))
console .log(arr1.myLastIndexOf(5 ,true ) == arr1.lastIndexOf(5 ,true ))
console .log(arr1.myLastIndexOf(5 ,NaN ) == arr1.lastIndexOf(5 ,NaN ))
console .log(arr1.myLastIndexOf(5 ,-1 ) == arr1.lastIndexOf(5 ,-1 ))
console .log(arr1.myLastIndexOf(5 ,-5 ) == arr1.lastIndexOf(5 ,-5 ))
複製程式碼
Array.prototype.from
Array .prototype.myFrom = function (arrayLike,mapFn,context ) {
context = context || window
mapFn = mapFn || function (item ) {return item}
var arr = []
if (arrayLike.forEach){
arrayLike.forEach((value )=> {
arr.push(mapFn.call(context,value))
})
}else {
var length = arrayLike.length
var i = 0
while (i<length) {
arr.push(mapFn.call(context,arrayLike[i]))
i++
}
}
return arr
}
console .log(Array .prototype.myFrom(arrLike))
console .log(Array .prototype.myFrom(set))
console .log(Array .prototype.myFrom(map))
複製程式碼
Array.prototype.of
Array .prototype.myOf = function ( ) {
var len = arguments .length
var arr =[]
arr.length = len
var i = 0
while (i < len) {
arr[i] = arguments [i]
i++
}
return arr
}
console .log(Array .prototype.myOf(1 ,2 ,3 ))
console .log(Array .prototype.myOf(undefined ))
console .log(Array .prototype.myOf(1 ))
複製程式碼
Array.prototype.copyWithin
Array .prototype.myCopyWithin = function (target,start,end ) {
var len = this .length
target = target < 0 ? Math .abs(target) > len ? len : len + target : target > len ? len : target
start = typeof start === 'number' ? start < 0 ? Math .abs(start) > len ? len : len + start : start > len ? len : start : 0
end = typeof end === 'number' ? end < 0 ? Math .abs(end) > len ? len : len + end : end > len ? len : end : len
var oTarget = target
var offset = end - start
var arr = Array .prototype.mySlice.call(this )
while (target < len && (target-oTarget) < offset && start < end) {
if (!this [start])break
this [target] = arr[start]
start++
target++
}
return this
}
console .log([1 , 2 , 3 , 4 , 5 ].myCopyWithin(-2 ));
console .log([1 , 2 , 3 , 4 , 5 ].copyWithin(-2 ));
console .log([1 , 2 , 3 , 4 , 5 ].myCopyWithin(0 , 3 ));
console .log([1 , 2 , 3 , 4 , 5 ].copyWithin(0 , 3 ));
console .log([1 , 2 , 3 , 4 , 5 ].myCopyWithin(0 , 3 , 4 ));
console .log([1 , 2 , 3 , 4 , 5 ].copyWithin(0 , 3 , 4 ));
console .log([1 , 2 , 3 , 4 , 5 ].myCopyWithin(-2 , -3 , -1 ));
console .log([1 , 2 , 3 , 4 , 5 ].copyWithin(-2 , -3 , -1 ));
console .log([1 , 2 , 3 , 4 , 5 ].myCopyWithin(3 , 2 , 4 ));
console .log([1 , 2 , 3 , 4 , 5 ].copyWithin(3 , 2 , 4 ));
console .log([].myCopyWithin.call({length : 5 , 3 : 1 }, 0 , 3 ));
console .log([].copyWithin.call({length : 5 , 3 : 1 }, 0 , 3 ));
console .log([].myCopyWithin.call(new Int32Array ([1 , 2 , 3 , 4 , 5 ]), 0 , 3 , 4 ));
console .log([].copyWithin.call(new Int32Array ([1 , 2 , 3 , 4 , 5 ]), 0 , 3 , 4 ));
複製程式碼
Array.prototype.fill
Array .prototype.myFill = function (value,start,end ) {
var len = this .length
start = typeof start === 'number' ? start < 0 ? Math .abs(start) > len ? len : len + start : start > len ? len : start : 0
end = typeof end === 'number' ? end < 0 ? Math .abs(end) > len ? len : len + end : end > len ? len : end : len
while (start < end) {
this [start] = value
start++
}
return this
}
console .log([1 , 2 , 3 ].myFill(4 ))
console .log([1 , 2 , 3 ].myFill(4 , 1 ))
console .log([1 , 2 , 3 ].myFill(4 , 1 , 2 ))
console .log([1 , 2 , 3 ].myFill(4 , 1 , 1 ))
console .log([1 , 2 , 3 ].myFill(4 , 3 , 3 ))
console .log([1 , 2 , 3 ].myFill(4 , -3 , -2 ))
console .log([1 , 2 , 3 ].myFill(4 , NaN , NaN ))
console .log([1 , 2 , 3 ].myFill(4 , 3 , 5 ))
console .log(Array (3 ).myFill(4 ))
console .log(Array .prototype.myFill.call({ length : 3 }, 4 ))
複製程式碼
Array.prototype.includes
Array .prototype.myIncludes = function (valueToFind,findIndex ) {
var len = this .length
findIndex = typeof findIndex === 'number' ? findIndex < 0 ? Math .abs(findIndex) > len ? len : len + findIndex : findIndex > len ? len : findIndex : 0
while (findIndex < len) {
var now = this [findIndex]
if (valueToFind === now)return true
if (valueToFind !== valueToFind && now !== now)return true
findIndex++
}
return false
}
console .log([1 , 2 , 3 ].myIncludes(2 ))
console .log([1 , 2 , 3 ].myIncludes(4 ))
console .log([1 , 2 , 3 ].myIncludes(3 , 3 ))
console .log([1 , 2 , 3 ].myIncludes(3 , -1 ))
console .log([1 , 2 , NaN ].myIncludes(NaN ))
複製程式碼
Array.prototype.keys
Array .prototype.myKeys = function ( ) {
if (!typeof this === 'object' )return
var arr = null
var length = this .length
if (!length){
arr = []
for (const key in this ) {
if (this .hasOwnProperty(key)) {
arr.push(key)
}
}
}
var len = this .length || arr.length
var nextIndex = 0
return {
[Symbol .iterator]: function ( ) {
return {
next :function ( ) {
return nextIndex < len ? {value : length ? nextIndex++ : arr[nextIndex++], done :false } : {done :true }
}
}
}
}
}
var a = ["a" , "b" , "c" ].myKeys()
var b = Array .prototype.myKeys.call({0 :1 ,1 :2 ,length :2 })
var c = Array .prototype.myKeys.call({a :1 ,b :2 })
for (const value of a) {
console .log(value)
}
for (const value of b) {
console .log(value)
}
for (const value of c) {
console .log(value)
}
複製程式碼
Array.prototype.values
Array .prototype.myValues = function ( ) {
if (!typeof this === 'object' )return
var arr = this
if (!this .length){
arr = []
for (const key in this ) {
if (this .hasOwnProperty(key)) {
arr.push(this [key])
}
}
}
var len = this .length || arr.length
var nextIndex = 0
return {
[Symbol .iterator]: function ( ) {
return {
next :function ( ) {
return nextIndex < len ? {value : arr[nextIndex++], done :false } : {done :true }
}
}
}
}
}
var a = ["a" , 'b' , "c" ].myValues()
var b = Array .prototype.myValues.call({0 :1 ,1 :2 ,length :2 })
var c = Array .prototype.myValues.call({a :1 ,b :2 })
for (const value of a) {
console .log(value)
}
for (const value of b) {
console .log(value)
}
for (const value of c) {
console .log(value)
}
複製程式碼
Array.prototype.entries
Array .prototype.myEntries = function ( ) {
if (!typeof this === 'object' )return
var arr = this
var len = this .length || arr.length
var nextIndex = 0
return {
[Symbol .iterator]: function ( ) {
return {
next :function ( ) {
return nextIndex < len ? {value :[nextIndex,arr[nextIndex++]], done :false } : {done :true }
}
}
}
}
}
var a = ["a" , 'b' , "c" ].myEntries()
var b = Array .prototype.myEntries.call({0 :1 ,1 :2 ,length :2 })
for (const value of a) {
console .log(value)
}
for (const value of b) {
console .log(value)
}
複製程式碼