在这里,我编写了一些带有一类函数的JavaScript代码。
如何在不从函数返回的情况下获得结果?
我的代码:
class myArray{
constructor(){
this.length = 0;
this.data = {}
}
get(index){
console.log(1)
// return this.data[index]
}
push(item){
this.data[this.length] = item
this.length++;
// return this.length
}
pop(){
const lastItem = this.data[this.length - 1];
delete this.data[this.length - 1];
this.length--;
// return lastItem
}
delete(index){
const item = this.data[index];
this.shiftItems(index)
// return item
}
shiftItems(index){
for (let i = index; i < this.length - 1; i++){
this.data[i] = this.data[i + 1]
}
delete this.data[this.length-1]
this.length--;
}
}
const newArray = new myArray();
newArray.push('hey');
newArray.push('you');
newArray.push('!');
newArray.pop();
newArray.delete(1)
newArray.push('are');
newArray.push('nice');
console.log(newArray)
我的输出:
myArray { length: 3, data: { '0': 'hey', '1': 'are', '2': 'nice' } }
我无法从JSON格式的结果中得知结果。