Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save adammcarth/3e67061d07783ea092a4 to your computer and use it in GitHub Desktop.

Select an option

Save adammcarth/3e67061d07783ea092a4 to your computer and use it in GitHub Desktop.
// Program logic
function ShoppingList( options ) {
this.items = {};
this.defaults = options.defaults;
if ( this.defaults ) {
this.items = this.defaults;
}
}
ShoppingList.prototype.addItems = function( items ) {
for ( var item in items ) {
quantity = items[item];
this.items[item] = quantity;
}
}
ShoppingList.prototype.get = function() {
return this.items
}
ShoppingList.prototype.reset = function() {
this.items = {};
if ( this.defaults ) {
this.items = this.defaults;
}
}
// Now to use the functions
var List = new ShoppingList({
defaults: {
"fruit": 5,
"vegetables": 3,
"chips": 1
}
});
console.log(List.get());
// => Object { fruit: 5, vegetables: 3, chips: 1 }
List.reset();
console.log(List.get());
// => Object { fruit: 5, vegetables: 3, chips: 1 }
List.addItems({
"coke": 1
});
console.log(List.get());
// => Object { fruit: 5, vegetables: 3, chips: 1, coke: 1 }
List.reset();
console.log(List.get());
// => Object { fruit: 5, vegetables: 3, chips: 1, COKE: 1 } WTF - why does coke get added to this.defaults????
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment