16. Single Responsibility
Principle
„A class should have one,
and only one, reason to change.“
17. function Product(id, description) {
this.getId = function() {
return id;
};
this.getDescription = function() {
return description;
};
} (function() {
function addToCart() {
function Cart(eventAggregator) { var productId = $(this).attr('id');
var items = []; var product = $.grep(products, function(x) {
return x.getId() == productId;
this.addItem = function(item) { })[0];
items.push(item); cart.addItem(product);
};
} var newItem = $('<li></li>')
.html(product.getDescription())
var products = [ .attr('id-cart', product.getId())
new Product(1, "Star Wars Lego Ship"), .appendTo("#cart");
new Product(2, "Barbie Doll"), }
new Product(3, "Remote Control Airplane")],
cart = new Cart(); products.forEach(function(product) {
var newItem = $('<li></li>')
.html(product.getDescription())
.attr('id', product.getId())
.dblclick(addToCart)
.appendTo("#products");
});
})();
http://freshbrewedcode.com/derekgreer/2011/12/08/solid-javascript-single-responsibility-principle/
18. function Product(id, description) {
add the item to the cart
this.getId = function() {
};
return id;
when an item is clicked
this.getDescription = function() {
return description;
};
} (function() {
function addToCart() {
function Cart(eventAggregator) { var productId = $(this).attr('id');
updating the DOM to display
var items = []; var product = $.grep(products, function(x) {
return x.getId() == productId;
the newly added item
this.addItem = function(item) {
items.push(item);
})[0];
cart.addItem(product);
};
} var newItem = $('<li></li>')
.html(product.getDescription())
var products = [ .attr('id-cart', product.getId())
new Product(1, "Star Wars Lego Ship"), .appendTo("#cart");
new Product(2, "Barbie Doll"), }
new Product(3, "Remote Control Airplane")],
cart = new Cart(); products.forEach(function(product) {
populate the initial list of var newItem = $('<li></li>')
products on the page .html(product.getDescription())
.attr('id', product.getId())
.dblclick(addToCart)
.appendTo("#products");
});
})();
http://freshbrewedcode.com/derekgreer/2011/12/08/solid-javascript-single-responsibility-principle/