Javascript sub-methods

Javascript sub-methods

本文关键字:sub-methods Javascript      更新时间:2023-09-26

我正在努力学习Javascript。我正在制作一个杂货清单,添加、删除、计数和清除商品。工作的部分都来自以前的堆栈溢出建议。我越是使用它,就越不明白为什么其中任何一个都有效。我刚刚再次浏览了codeacademy JS曲目,但它与我在这里所做的不匹配。

我只讨论最后两种方法:计算价格和;搜索项目以返回其属性。当我改变这些方法来尝试做一些简单的事情,比如打印出一个数字时,这甚至都不起作用。鲁比远没有这么痛苦。我会接受任何关于理解JS或如何以及为什么我剩下的方法不起作用的建议。

var groceryList = function(food, quantity, price) {
    this.items = [];
    this.items.push({food:food, quantity:quantity, price:price});
    var currentList = this;
    this.addStuff = function(food, quantity, price) {
        currentList.items.push({food:food, quantity:quantity, price:price});
        return currentList.items;
    };
    this.tallyList = function() {
        return currentList.items.length;
    };
    this.getItems = function() {
        return currentList.items;
    };

// BROKEN - always returns 'not on list' - the matching bit does not work
    this.onList = function(someItem) {
      for(var key in currentList) {
        console.log(currentList[key] === someItem); // t/f
        if(currentList.items === someItem) {
          return currentList[key];
        }
        else {
          return "not on list"
        }
      }
    };
// BROKEN -the simplest of commands here will not print.  WTF?
    this.whatWillItCost = function() {
        mySum = 0;
        for(i = 0; i < currentList.items.length; i++) {
          mySum += currentList.items.price;
        }
        return mySum;
    };
    this.goShopping = function() {
        currentList.items = {};
        return currentList.items;
    };
};

//TESTS
var myList = new groceryList("cookie", 2, 1.00);
console.log(myList);
myList.addStuff("brownie", 1, 2.50);
console.log(myList);
console.log(myList.tallyList());
console.log(myList.onList("cookie")); // must fix - always returns false
console.log(myList.whatWillItCost) // must fix - totally broken
console.log(myList.goShopping());

对于第一个函数,您引用的是类,而不是列表中的项(您在第二个函数中做得很好),所以它应该是:

this.onList = function(someItem) {
  for(i = 0; i < currentList.items.length; i++) {
    if(currentList.items[i].food === someItem) {
      return currentList.items[i]
    }
    else {
      return "not on list"
    }
  }
};

至于你的第二个,唯一的错误是缺少对项目列表索引的引用:

mySum += currentList.items[i].price; // should it be * currentList.items[i].quantity?

您还需要更改:

console.log(myList.whatWillItCost()) // without the () you are displaying the reference to the function instead of executing it

Js Fiddle:http://jsfiddle.net/3a4ebdpz/

您也可以使用Array.filter:来缩短onList函数

this.onList = function(someItem) {
    var results= currentList.items.filter(function(item){ 
        return item.food===someItem;
    })
    return results.length == 0 ? 'not on list' : results[0]
};

它正在工作,"我只剩下最后两种方法:计算价格和搜索物品以返回其属性"已经修复。

PFB代码,

<html>
<body>
<script>
var groceryList = function(food, quantity, price) {
    this.items = [];
    this.items.push({food:food, quantity:quantity, price:price});
    var currentList = this;
    this.addStuff = function(food, quantity, price) {
        currentList.items.push({food:food, quantity:quantity, price:price});
        return currentList.items;
    };
    this.tallyList = function() {
        return currentList.items.length;
    };
    this.getItems = function() {
        return currentList.items;
    };

// BROKEN - always returns 'not on list' - the matching bit does not work
    this.onList = function(someItem) {
      for(var key in currentList.items) {
        console.log(currentList.items[key].food === someItem); // t/f
        if(currentList.items[key].food === someItem) {
          return currentList.items[key];
        }
        else {
          return "not on list"
        }
      }
    };
// BROKEN -the simplest of commands here will not print.  WTF?
    this.whatWillItCost = function() {
        mySum = 0;
        for(i = 0; i < currentList.items.length; i++) {
          mySum += currentList.items[i].price;
        }
        return mySum;
    };
    this.goShopping = function() {
        currentList.items = {};
        return currentList.items;
    };
};

//TESTS
var myList = new groceryList("cookie", 2, 1.00);
console.log(myList.items);
myList.addStuff("brownie", 1, 2.50);
console.log(myList.items);
console.log(myList.tallyList());
console.log(myList.onList("cookie")); // must fix - always returns false
console.log(myList.whatWillItCost()) // must fix - totally broken
console.log(myList.goShopping());
</script>
</body>
</html>