You have a few things wrong:
listPhotos.push
should bethis.listPhotos.push
- You should be pushing the variable
x
and not the stringx
- When logging you want
this.listPhotos
and notx.listPhotos
function Album() {
this.listPhotos = ["bee", "ladybug", "caterpillar", "ant"];
this.addPhoto = function(x) {
this.listPhotos.push(x);
console.log(this.listPhotos);
}
}
let a = new Album()
a.addPhoto('123')
solved How to write a function that takes an argument and adds it to an array? [closed]