Javascript子方法

时间:2014-10-15 17:19:50

标签: javascript nested

我正在尝试学习Javascript。我正在制作一个可以添加,删除,计算和清除项目的购物清单。有效的部分都来自之前的堆栈溢出建议。我使用它越多,我就越不明白为什么它有效。我刚刚再次浏览了codeacademy JS轨道,但它与我在这里所做的并不匹配。

我采用最后两种方法:计算价格和价格。搜索项目以返回其属性。当我改变这些方法尝试做一些简单的事情,比如打印出一个数字,这甚至不会起作用。 Ruby无法学到这种痛苦。我会就理解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());

2 个答案:

答案 0 :(得分:1)

对于第一个函数,您引用的是类而不是列表中的项(在第二个函数中表现良好),因此它应该是:

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]
};

答案 1 :(得分:0)

它的工作,&#34;我采用了最后两种方法:计算价格和价格。搜索项目以返回其属性&#34;已被修复。

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>