【发布时间】:2015-11-10 19:26:18
【问题描述】:
我正在尝试对数组方法 push、pull、shift、unshift 进行反向工程,但我似乎无法弄清楚如何 a) 构造它 b) 调用它。我怎么能这样做?以下是条件:
返回一个空数组对象。这个对象应该有 以下方法: push(val) 将 val 添加到数组的末尾 pop() 从末尾删除一个值并返回它 unshift(val) 添加 val 到数组的开头 shift() 从 开始并返回它这个问题的目标是扭转 设计数组方法实际在做什么并返回一个对象 有那些方法
这是我最初认为的样子。
function createArray() {
//CODE HERE
this.push = function Push(value){
if(index >= 0){
Mainarray[index++]=value;}
};
this.pop = function (){
if(index >= 0){
index--;
return Mainarray[index];
}
else{
// display message of Empty Array
console.log('Error: Item is not array');
}
};
this.unshift = function(){return ;};
}
【问题讨论】:
标签: javascript arrays methods