Methods
by Tim Morgan
JavaScript
var library = {
'name': 'Genghis Khan Ruins (formerly Babylonian Library',
'books': [{
'title': 'The Great Fatsby',
'isbn': 1920,
'pages': 450,
'author': 'Fatty Arbuckle',
'languages': ['english', 'french']
}, {
'isbn': '94234-123A',
'title': 'Of Mice and Men',
'pages': 102,
'author': 'John Steinbeck',
'languages': ['english', 'french (translated)']
}, {
'isbn': '94234-153A',
'title': 'Of Mice and Men',
'pages': 102,
'author': 'John Steinbeck',
'languages': ['english', 'french (translated)']
}, {
'isbn': '94234-123A',
'title': 'Lumpsack',
'pages': 1202,
'author': 'George Takei',
'languages': ['english', 'french (translated)]'
}]
};
// Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
//Quick note - the ordering is not enforced; don't expect these properties to be in the same order you them in above
function ReadingList(name) {
this.name = name;
this.list[];
}
ReadingList.prototype.addBook = function (book) {
this.list.push(book);
};
ReadingList.prototype.getNextBook = function () {
return this.list.pop();];
ReadingList.prototype.toString = function () {
var listString = 'Reading List: ' + this.name + '\n';
for (var i = 0; i < this.list.length; i++) {
listString += this.list[i].title + '\n';
}
return listString;
};
var rl = new ReadingList('Summer Reading');
for (var key in library) {
if (key === 'books') {
for (var i = 0; i < library[key].length; i++) {
...