将方法添加到操纵数组的构造函数

时间:2016-01-22 18:50:08

标签: javascript arrays methods constructor prototype

我正在尝试创建一个简单的练习程序,它有一个Store构造函数,它接受一个inventoryList数组,一个InventoryItem构造函数,用于创建要存储的每个项目的对象在inventoryList数组中。我想在InventoryItem列表中添加一个方法,该方法将获取一个数字并将其添加到列表中的每个项目。以下是我的代码:

var Store = function(inventory){
    this.inventory = inventory;
}

var InventoryItem = function(cost, color, size){
    this.cost = cost;
    this.color = color;
    this.size = size;
}

//create each inventory item as an object

var lamp = new InventoryItem(40.00, 'blue', 'small');
var throwPillow  = new InventoryItem(25.00, 'yellow', 'medium');
var sconce = new InventoryItem( 18.00, 'gold', 'large');
var candles = new InventoryItem(10.00, 'white', 'small');
var curtains = new InventoryItem(30.00, 'purple', 'large');

//store inventory items into an inventory object

var inventorylist = [lamp, throwPillow, sconce, candles, curtains];

// add number of items to each item

InventoryItem.prototype.howMany = function(num){
    function addCount(inventorylist){
        for(var i = 0; i < inventorylist.length; i++){
            inventorylist[i].howMany = num;
        }
    }
}

//store inventory within a store
var deannaStore = new Store(inventorylist);
var dionStore = new Store(inventorylist);
var mikeStore = new Store(inventorylist);

当我尝试向列表中的项目添加数字时,我遇到了问题 例如:dionStore.howMany(15)

这给了我错误:Uncaught TypeError: dionStore.howMany is not a function

任何帮助都会受到高度赞赏,最好详细解释为什么这不起作用。

2 个答案:

答案 0 :(得分:1)

问题是方法howMany是为InventoryItem个对象定义的,但您尝试将其调用dionStore Store对象。

您能描述一下您想要达到的目标吗?

EDIT1:

也许将howMany方法添加到Store,如 Lashane 提及,这将为其数组中的每个quantity创建一个InvetoryItem属性

Store.prototype.howMany = function(num){
    function addCount(inventorylist){
        for(var i = 0; i < inventorylist.length; i++){
            inventorylist[i].quantity = num;
        }
    }
}

EDIT2:

在这种情况下,您需要InventoryItem中的方法来设置数量,而另一个方法需要Store来返回总数量

Store.prototype.howMany = function(){
    var totalQuantity = 0;

    for(var i = 0; i < inventorylist.length; i++){
        var item = inventorylist[i];

        if (item.hasOwnProperty('quantity')) {
            totalQuantity += item.quantity;
        }
    }

    return totalQuantity;
}

InventoryItem.prototype.addQuantity = function(quantity) {
    if (this.hasOwnProperty('quantity')) {
        this.quantity += quantity;
    } else {
        this.quantity = quantity;
    }
}

var Store = function(inventory) {
  this.inventory = inventory;
}

Store.prototype.howMany = function() {
  var totalQuantity = 0;

  for (var i = 0; i < inventorylist.length; i++) {
    var item = inventorylist[i];

    if (item.hasOwnProperty('quantity')) {
      totalQuantity += item.quantity;
    }
  }

  return totalQuantity;
}

var InventoryItem = function(cost, color, size) {
  this.cost = cost;
  this.color = color;
  this.size = size;
}

InventoryItem.prototype.addQuantity = function(qunatity) {
  if (this.hasOwnProperty('quantity')) {
    this.quantity += qunatity;
  } else {
    this.quantity = qunatity;
  }
}


//create each inventory item as an object

var lamp = new InventoryItem(40.00, 'blue', 'small');
var throwPillow = new InventoryItem(25.00, 'yellow', 'medium');
var sconce = new InventoryItem(18.00, 'gold', 'large');
var candles = new InventoryItem(10.00, 'white', 'small');
var curtains = new InventoryItem(30.00, 'purple', 'large');

//store inventory items into an inventory object

var inventorylist = [lamp, throwPillow, sconce, candles, curtains];

lamp.addQuantity(1);
throwPillow.addQuantity(2);
sconce.addQuantity(3);
candles.addQuantity(4);
curtains.addQuantity(5);

lamp.addQuantity(1);

//store inventory within a store
var deannaStore = new Store(inventorylist);
var dionStore = new Store(inventorylist);
var mikeStore = new Store(inventorylist);

document.getElementById('output').innerHTML = 'dionStore.howMany() = ' + dionStore.howMany();
<div id="output"></div>

答案 1 :(得分:0)

您的原型定义了一个函数并且什么都不做。这是错误的原型。你在找这个吗?

Store.prototype.howMany = function(num){ 
    for(var i = 0; i < this.inventory.length; i++){
        this.inventory[i].howMany = num;
    }
}