Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重构javascript_Javascript_Angularjs - Fatal编程技术网

重构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;

我有一个角度控制器中的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;
    } 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.
}