Reputation: 291
I am studying through Eloquent Javascript, and I have the following code as one of the exercices.
class Group {
constructor(){
this.arr = []
}
add(value){
if(!this.has(value)) {
this.arr = this.arr.push(value)
}
}
has(value){
return this.arr.includes(value);
}
delete(value){
this.arr = this.arr.filter(n => n !== value)
}
static from(collection){
let rec = new Group;
for (let value of collection){
rec.add(value)
}
return rec
}
}
It seems right and it should be working but I get the error
TypeError: arr.includes is not a function
What is that about? I can't find the answer.
Upvotes: 0
Views: 7983
Reputation: 28455
From docs,
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Hence, the problem is with this line. this.arr
is no longer an array after this line.
this.arr = this.arr.push(value)
So, update this to following
this.arr.push(value)
Upvotes: 2