We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度
语法
arr.push(element1, ..., elementN)
参数
elementN: 被添加到数组末尾的元素。
返回值
当调用该方法时,新的 length 属性值将被返回
Array.prototype.push2 = function(){ let O = Object(this),len = O.length >>> 0; let argsLength = arguments.length >>> 0; let k = 0; while(argsLength--){ O[len] = arguments[k++]; len++; } return len; } var vegetables = ['parsnip', 'potato']; var moreVegs = ['celery', 'beetroot']; // 将第二个数组融合进第一个数组 // 相当于 vegetables.push('celery', 'beetroot'); Array.prototype.push2.apply(vegetables, moreVegs); console.log(vegetables);
pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度
arr.pop()
从数组中删除的元素(当数组为空时返回undefined)。
Array.prototype.pop2 = function(){ let O = Object(this),len = O.length >>> 0; let result; if(len){ len--; result = O[len]; delete O[len] O.length = len; } return result; } var arr = [1,2,3]; console.log( arr.pop2(), arr )
The text was updated successfully, but these errors were encountered:
No branches or pull requests
push
语法
参数
返回值
push模拟实现
pop
语法
返回值
pop模拟实现
The text was updated successfully, but these errors were encountered: