Like many, I am using prototype inheritance to define object methods. This is a small sample of code that I believe should work:
// bag object
function bag() {
this.list = [];
}
// bag methods
bag.prototype = {
stack: function (e) {
this.list.unshift(e);
return this;
},
stackAll: function (es) {
es.forEach(this.stack); // refer stack method
return this;
}
}
let b = new bag();
b.stackAll([1,2,3]); // error: no method this.stack
console.log(b);
Depending on interpreters I have had either of two errors: - not finding the method this.stack (when called from stackAll) - not finding this.list (when referred to in stack() from stackAll)
I'm puzzled. I'm interested in an alternative that would work, and a rationale why it doesn't.