Skip to content

Instantly share code, notes, and snippets.

@jamesnvc
Last active April 12, 2019 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesnvc/5a58fe57c8a5c5724edf3a2df01a2f58 to your computer and use it in GitHub Desktop.
Save jamesnvc/5a58fe57c8a5c5724edf3a2df01a2f58 to your computer and use it in GitHub Desktop.
LHL W6D5 - 2019-04-12
class ShoppingCart {
constructor(name) {
this.name = name;
this.items = [];
}
addItem(item) {
this.items.unshift(item);
}
get count() {
return this.items.length;
}
}
let cart = new ShoppingCart('james');
cart.addItem('socks');
cart.addItem('pants');
console.log(cart, cart.count);
class OneSpoonCart extends ShoppingCart {
constructor(name, limit) {
super(name);
this.limit = limit;
}
addItem(item) {
super.addItem(item);
this.items = this.items.slice(0, this.limit);
}
}
let cart2 = new OneSpoonCart("james", 3);
console.log('limited cart', cart2);
cart2.addItem('socks');
cart2.addItem('hat');
cart2.addItem('pants');
OneSpoonCart.prototype.addItem = function(item) {
console.log("i refuse");
};
cart2.addItem('gloves');
console.log('limited cart', cart2);
console.log(cart2.count, "items");
Array.prototype.whatever = function() {
return this.join("\n");
};
const ShoppingCart = function(name) {
this.name = name;
this.items = [];
return this;
};
ShoppingCart.prototype.addItem = function(item) {
this.items.unshift(item);
};
Object.defineProperty(ShoppingCart.prototype,
"count",
{get() { return this.items.length; }});
let cart = new ShoppingCart('james');
cart.addItem('socks');
cart.addItem('pants');
let cart2 = new ShoppingCart('semaj');
cart2.addItem('socks');
console.log(cart);
console.log(cart2);
// hm, I want to add some logging to addItem
ShoppingCart.prototype.addItem = function(item) {
console.log(`adding item ${item} to ${this}`);
this.items.push(item);
};
cart2.addItem('hat');
console.log(cart2.count, "items in cart2");
const OneSpoonCart = function(name, limit) {
ShoppingCart.call(this, name);
this.limit = limit;
return this;
};
OneSpoonCart.prototype = Object.create(ShoppingCart.prototype);
OneSpoonCart.prototype.constructor = OneSpoonCart;
OneSpoonCart.prototype.addItem = function(item) {
ShoppingCart.prototype.addItem.call(this, item);
this.items = this.items.slice(0, this.limit);
};
let cart3 = new OneSpoonCart("james", 3);
console.log('limited cart', cart3);
cart3.addItem('socks');
cart3.addItem('hat');
cart3.addItem('pants');
cart3.addItem('gloves');
console.log('limited cart', cart3);
console.log(cart3.count, "items");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment