I'm trying to build a reusable Database
class with prototypical methods to add items to the class's list
array property:
var Database = function() {
this.list = [];
}
Database.prototype = {
getBooks: function () {
$.get("//some.url/books")
.success(function(res) {
_.each(res,function(book) {
this.list.push({
name: book.name,
url: book.url,
type: "book"
})
})
});
}
}
However, currently I get a warning saying that I cannot push to undefined property list
, and I can only assume that I'm not actually .push
ing to any Database
class instance property, but rather to the prototype property.
So, the question restated: How do you use a prototype method to change an instance's properties?