i am trying to push a class to an empty array . then use for to loop through classes in this array but it is not working. is there another way to loop through classes?nothing return in the console , no errors shown
let a=[]
class push_array{
constructor (first , second) {
this.first=first;
this.second=second
function pushing(){
a.push(this)
pushing()
console.log(a)
}
}
}
const result=new push_array('fdsfs','fdsfdsf')
>Solution :
pushing is called from within pushing, so the logic that pushes the instance to the array is never executed.
let a = []
class push_array {
constructor(first, second) {
this.first = first;
this.second = second;
a.push(this);
}
}
const result = new push_array('fdsfs', 'fdsfdsf');
console.log(a);
Its unclear what your goal is, but using variable outside of a class could make your code complex very fast. If you’re storing the result, why not just push the object to the array after instantiating?
let a = []
class push_array {
constructor(first, second) {
this.first = first;
this.second = second;
}
}
const result = new push_array('fdsfs', 'fdsfdsf');
a.push(result);
console.log(a);