Skip to content

Instantly share code, notes, and snippets.

@bholzer
Created October 17, 2013 01:42
Show Gist options
  • Save bholzer/7017997 to your computer and use it in GitHub Desktop.
Save bholzer/7017997 to your computer and use it in GitHub Desktop.
function Product (options) {
//I like to have all my instance variables in one accessible place
this.priceChangeListeners = new Array();
this.name = null;
this.price = 0.0;
this.upc = null;
this.image_path = null;
// Was: if (options.name == undefined)
// In javascript, many values evaluate to false, including undefined
// These are known as falsy values, and it can be helpful to check them all at once
if (options.name) {
this.name = "TBD";
} else {
this.name = options.name;
}
// Was: if (options.price > 0)
// Always try to use decimals for any values that may become decimals
// Also, check for the existence of the property before doing any comparisons
if (options.price && options.price > 0.0) {
//Math.round(options); <- options is just a javascript object, which can't be rounded i.e. {price: 10.34, name: "Test"}
this.price = options.price;
}
if (typeof options.upc == 'string') {
this.upc = new UPC(options.upc);
} else if (typeof options.upc == 'object') {
this.upc = options.upc;
// Was: else if (options.upc == null)
// The property you are trying to access will return undefined if it isn't there, not null
} else if (typeof options.upc == 'undefined') {
this.upc = new UPC();
}
if (options.image_path[0] == '/'){
var j = options.image_path.search(".jpg");
var p = options.image_path.search(".png");
var g = options.image_path.search(".gif");
var jp= options.image_path.search(".jpeg");
}
if (j > -1 || p > -1 || g > -1 || jp > -1)
this.image_path = options.image_path;
}
}
Product.prototype.addPriceChangeListener = function(listener) {
this.priceChangeListeners.push(listener);
//Return the index of the listener, so we can have a reference to remove it with
return this.priceChangeListeners.length-1;
};
Product.prototype.removePriceChangeListener = function(listenerIndex){
//Set the listener in array to undefined
//Because of this, we also need to add a check in setPrice to make sure the function is defined before trying to call it
delete this.priceChangeListeners[listenerIndex];
};
Product.prototype.setPrice = function(newPrice) {
if (newPrice > 0.0) {
this.price = newPrice;
for (var i = 0; i < this.priceChangeListeners.length; i++){
if (this.priceChangeListeners[i]) {
this.priceChangeListeners[i](newPrice, this);
}
}
}
};
Product.prototype.getPrice = function() {
return this.price;
}''
function Product (options) {
this.priceChangeListeners = new Array();
this.name = null;
this.price = 0.0;
this.upc = null;
this.image_path = null;
if (options.name) {
this.name = "TBD";
} else {
this.name = options.name;
}
if (options.price && options.price > 0.0) {
this.price = options.price;
}
if (typeof options.upc == 'string') {
this.upc = new UPC(options.upc);
} else if (typeof options.upc == 'object') {
this.upc = options.upc;
} else if (typeof options.upc == 'undefined') {
this.upc = new UPC();
}
if (options.image_path[0] == '/'){
var j = options.image_path.search(".jpg");
var p = options.image_path.search(".png");
var g = options.image_path.search(".gif");
var jp= options.image_path.search(".jpeg");
}
if (j > -1 || p > -1 || g > -1 || jp > -1)
this.image_path = options.image_path;
}
}
Product.prototype.addPriceChangeListener = function(listener) {
this.priceChangeListeners.push(listener);
return this.priceChangeListeners.length-1;
};
Product.prototype.removePriceChangeListener = function(listenerIndex){
delete this.priceChangeListeners[listenerIndex];
};
Product.prototype.setPrice = function(newPrice) {
if (newPrice > 0.0) {
this.price = newPrice;
for (var i = 0; i < this.priceChangeListeners.length; i++){
if (this.priceChangeListeners[i]) {
this.priceChangeListeners[i](newPrice, this);
}
}
}
};
Product.prototype.getPrice = function() {
return this.price;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment