1

クラスプロジェクトのコードを書いていますが、次のコードで問題が発生しています。

    var sellEverything = function(gold)
{
    console.log("You decide to buy EVERYTHING.");

    var holder;
    while (storeStock.length > 0)
    {
        console.log(storeStock);
        var curItem = storeStock.pop();
        console.log(curItem);
        console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
        gold -= curItem.itemPrice;
        console.log(gold + " gold remaining");
    };
};

観点から見ると、storeStockは6つのオブジェクトを含む配列です。curItem変数が未定義としてコンソールに表示されるので、配列からオブジェクトをポップすることさえ可能ですか、それとも問題は別のものですか?

storeStockに入力されるデータは次のとおりです。

    "items":
    [
        {"itemName":"Sword", "itemPrice":100},
        {"itemName":"Bow", "itemPrice":240},
        {"itemName":"Shield", "itemPrice":120},
        {"itemName":"Lance", "itemPrice":300},
        {"itemName":"Potion", "itemPrice":50},
        {"itemName":"Gem of supreme power", "itemPrice":5},
        {"itemName":"Better movie script", "itemPrice":139083882}
    ]

明らかな質問の場合は事前に申し訳ありません。

編集:わかりました。わかりやすくするために、コード全体を表示する必要があると思います。

var itemList =
{
"items":
    [
        {"itemName":"Sword", "itemPrice":100},
        {"itemName":"Bow", "itemPrice":240},
        {"itemName":"Shield", "itemPrice":120},
        {"itemName":"Lance", "itemPrice":300},
        {"itemName":"Potion", "itemPrice":50},
        {"itemName":"Gem of supreme power", "itemPrice":5},
        {"itemName":"Better movie script", "itemPrice":139083882}
    ]
};

//private class ItemStore
var ItemStore = function()
{
    var storeStock = [];

    //array in function
    var setStockList = function(stockArray)
    {
        if (stockArray instanceof Array)
        {
            var i = stockArray.length;
            for (i; i > 0; i--)
            {
                storeStock.push(stockArray[i]);
            }
            //yes, I could have just done the loop forwards, but wanted to do it this way.
            storeStock.reverse(); 
        };
    };

    //array out function
    var getStockList = function()
    {
        return (storeStock);
    };

    var sellEverything = function(gold)
    {
        console.log("You decide to buy EVERYTHING.");

        var holder;
        while (storeStock.length > 0)
        {
            console.log(storeStock);
            var curItem = storeStock.pop();
            console.log(curItem);
            console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
            gold -= curItem.itemPrice;
            console.log(gold + " gold remaining");
        };
    };

    return{
        "setStockList": setStockList,
        "sellEverything": sellEverything,
        "getStockList":getStockList
    };
};

また、setStockListメソッドの呼び出し方法にも注意してください。

    store.setStockList(itemList.items);
4

3 に答える 3

2
var itemList = {
    items:[
        {itemName: "Sword", itemPrice: 100},
        {itemName: "Bow", itemPrice: 240},
        {itemName: "Shield", itemPrice: 120},
        {itemName: "Lance", itemPrice: 300},
        {itemName: "Potion", itemPrice: 50},
        {itemName: "Gem of supreme power", itemPrice: 5},
        {itemName: "Better movie script", itemPrice: 139083882}
    ]};

//private class ItemStore
var ItemStore = function()
{
    var storeStock = [];

    //array in function
    var setStockList = function(stock)
    {
        if (stock instanceof Array)
        {
            for (var item in stock)
            {
                storeStock.push(stock[item]);
            }

            storeStock.reverse(); 
        };
    };

    //array out function
    var getStockList = function()
    {
        return storeStock;
    };

    var sellEverything = function(gold)
    {
        var holder;

        console.log("You decide to buy EVERYTHING.");

        while (curItem = storeStock.pop())
        {
            console.log(curItem);
            console.log(
                "You buy a " + 
                curItem.itemName + 
                " for " + 
                curItem.itemPrice + 
                " gold."
            );

            gold -= curItem.itemPrice;

            console.log(gold + " gold remaining");
        };
    };

    return {
        setStockList: setStockList,
        sellEverything: sellEverything,
        getStockList: getStockList
    };
};


var store = new ItemStore();
store.setStockList(itemList.items);
console.log(store.getStockList());
store.sellEverything(100000);

http://jsfiddle.net/userdude/sS2fY/3/

于 2012-08-16T01:40:34.960 に答える
1

配列storeStockを持つオブジェクトのようです。itemsもしそうなら、これはうまくいくはずです:

var sellEverything = function(gold)
{
    console.log("You decide to buy EVERYTHING.");

    var holder;
    while (storeStock.items.length > 0)
    {
        console.log(storeStock);
        var curItem = storeStock.items.pop();
        console.log(curItem);
        console.log("You buy a " + curItem.itemName + " for " + curItem.itemPrice + " gold.");
        gold -= curItem.itemPrice;
        console.log(gold + " gold remaining");
    };
};
于 2012-08-16T01:39:18.593 に答える
0

storeStockstoreStock.items必要な配列であるオブジェクトです

于 2012-08-16T01:39:27.757 に答える