重构javascript
我有一个角度控制器中的javascript代码,可以很好地工作重构javascript,javascript,angularjs,Javascript,Angularjs,我有一个角度控制器中的javascript代码,可以很好地工作 product.prod_custom_mb_qty = 0; product.prod_custom_mb = null; product.prod_custom_autoship = function (prod_name, prod_qty) { if (prod_name === "mb") { if (prod_qty === 0) { product.prod_custom_mb_qty = 0;
product.prod_custom_mb_qty = 0;
product.prod_custom_mb = null;
product.prod_custom_autoship = function (prod_name, prod_qty) {
if (prod_name === "mb") {
if (prod_qty === 0) {
product.prod_custom_mb_qty = 0;
} else {
product.prod_custom_mb_qty += prod_qty;
}
}
if (product.prod_custom_mb_qty < 1){
product.prod_custom_mb_qty = 0;
product.prod_custom_mb = null;
} else if (product.prod_custom_mb_qty === 1) {
product.prod_custom_mb = product.prod_custom_mb_qty + " Mango Bottle";
} else if (product.prod_custom_mb_qty > 1) {
product.prod_custom_mb = product.prod_custom_mb_qty + " Mango Bottles";
}
product.prod_custom_total = (product.prod_custom_mb_qty * 35);
}
但我好像没法让它发挥作用?我走对了吗?我建议这样组织:
var products = [];
var productCount = 8;
while(productionCount--) {
var newProduct = {
prod_custom_mb_qty: 0,
prod_custom_mb: null
};
pruducts.push(newProduct);
processProduct(newProduct);
}
function processProduct (product) {
// Update your product properties here.
}
var products = [];
var productCount = 8;
while(productionCount--) {
var newProduct = {
prod_custom_mb_qty: 0,
prod_custom_mb: null
};
pruducts.push(newProduct);
processProduct(newProduct);
}
function processProduct (product) {
// Update your product properties here.
}