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>
相关文章:
- JavaScript Pub/Sub属性访问问题
- babel with sub js files
- Difference between methods of defining JavaScript 'class
- 何时使用Meteor.methods和使用存根
- three.js:three.Vector3.sub()动作怪异
- 使用中介pub/sub模式时路由骨干应用程序
- Class methods in node.js
- Cross Sub Domain Cookie
- 如何使此pub/sub代码可读性更强
- underscore.js groupBy sub-array
- Addy Osmanis pub/sub,难以理解代码
- 为什么我在尝试使用 StageWebView 在 AIR Android 应用程序中实现 Google Maps Jav
- Openfire 会在一天后删除 pub sub 节点(而不是在服务器重新启动或删除缓存后)
- Meteor.methods:从内部回调将数据返回给客户端
- javascript document.queryCommandState() “superscript” / “sub
- 未捕获的类型错误:无法读取 Google Analytics API 中 null 的属性“sub”
- redis sub/pub in or out of io.connect callback
- RequireJS, Pub/Sub.如果没有糟糕的解决方法,我无法从其自己的方法访问视图实例
- Javascript methods: this & that
- Javascript sub-methods