In constructor function I returned this.ar, and in push method I returned item, but the problem is when I return from constructor and push both, only the constructor returns, and push returns 1,2,3… is there any solution?
I want when user console the class then print the array — (this.ar).
Note: IIFE not working in class object.
class Arr{
constructor(ar){
this.top = -1;
this.ar = ar;
return this.ar;
}
push(item){
if(this.ar.length >= 0 ){
this.top = this.ar.length - 1;
this.top++;
this.ar[this.top] = item;
return item;
}
else{
return "Something went wrong";
}
}
}
const stack = [];
const array = new Arr(stack);
console.log(array.push(3));
console.log(array);
I tried IIFE which not gonna work in class, and I returned this.ar from direct class, it’s also not works. I want the output – show code comment right side.
const stack = [];
const array = new Arr(stack);
console.log(array.push(3)); // 3
console.log(array.push(4)); // 4
console.log(array); // [3,4]