赞
踩
目录
- Array.prototype.push=function(){
- for(let i=0;i<arguments.length;i++){
- this[this.length]=arguments[i]
- }
- return this.length
- }
- const arr=[1,2,3]
- console.log(arr.push(4,5,6))
- Array.prototype.unshift = function () {
- const args = [...arguments]
- const oldArray = [...this]
- for (let i = 0; i < args.length; i++) {
- this[i] = args[i]
- }
- for (let j = 0; j < oldArray.length; j++) {
- this[this.length] = oldArray[j]
- }
- return this.length
- }
- const arr = [1, 2, 3]
- console.log(arr.unshift(6, 5, 4))
- Array.prototype.pop=function(){
- if(this.length===0)return undefined
- const lastItem=this[this.length-1]
- this.length--
- return lastItem
- }
- const arr=[1,3,4]
- console.log(arr.pop())
- Array.prototype.shift=function(){
- if(this.length===0)return undefined
- const firstItem=this[0]
- for(let i=0;i<this.length;i++){
- this[i-1]=this[i]
- }
- this.length--
- return firstItem
- }
- const arr=[1,2,3]
- console.log(arr.shift())
- Array.prototype.map=function(fn){
- const newArr=[]
- for(let i=0;i<this.length;i++){
- const r=fn(this[i],i,arr)
- newArr[newArr.length]=r
- }
- return newArr
- }
- arr=[8,5,7,9]
- console.log(arr.map(function(item){
- return item*2
-
- }))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。