基于对象属性修改全局变量

时间:2013-10-10 07:06:10

标签: javascript

我正在尝试学习面向对象的javascript并遇到以下问题:

我有一个对象构造函数(这是正确的术语?),如下所示:

function itemCreator(itemName, itemType, itemPositionX, itemPositionY) {
            this.itemName = itemName;
            this.itemType = itemType;
            this.itemPositionX = itemPositionX;
            this.itemPositionY = itemPositionY;
            allItems.push(this); //store all items in a global variable
            }//end itemCreator;


//then I use it to create an object

megaRocket = new itemCreator (
           'megarocket',
           'item_megarocket',
           108,
           475
           )

现在我意识到我还需要根据对象具有的“itemType”来映射这些对象以修改不同的全局变量。这是我被困的地方。如何创建一个全局变量,只有具有特定itemType属性的对象才能修改?

例如,我想创建一个对象,该对象增加一个名为amountOfMegarockets的变量,但前提是该对象的itemType是“item_megarocket”。

我后来计划循环这些项目的数组以查看玩家对象是否接触它们(收集项目):

function checkForItems(){ 
                var itemLen =allItems.length;
                for (i=0; i < itemLen; i++){
                var itemObject = allItems[i];

                if ( //checking for "collisions" here
                (ship.x < (itemObject.itemBitmap.x + itemObject.size) &&  (ship.x + shipWidth) > itemObject.itemBitmap.x) &&
                (ship.y < (itemObject.itemBitmap.y + itemObject.size) &&  (ship.y + shipWidth) > itemObject.itemBitmap.y)
                ){
                itemObject.actor.y = -500; //just removing the item from canvas here (temporary solution)
                // Here comes pseudo code for the part that I'm stuck with  
                variableBasedOnItemObject.itemType++;

                }

我希望我的解释对某人有意义!

编辑:

Bergi的回答对我来说最有意义,但我无法正确理解语法。以下是我尝试使用Bergi代码的方法:

var amounts = {},
allItems = [];
function itemCreator(itemName, itemType, itemPositionX, itemPositionY) {
this.itemName = itemName;
this.itemType = itemType;
this.itemPositionX = itemPositionX;
this.itemPositionY = itemPositionY;

(amounts[itemType]=2); // this is different from bergi's example because I need to set the initial value of the item to two
//I also shouldn't increase the item amount on creation of the item, but only when it's specifically called from another function
this.increaseCount = amounts[itemType]++; //this should IMO increase the itemType amount inside the amounts object when called, but it doesn't seem to work
}

//creating the object the way bergi suggested:
allItems.push(new itemCreator('shootUp001', 'item_up', 108, 475)); 

现在这是有问题的部分:

function checkForItems(){ 
                var itemLen =allItems.length;
                for (i=0; i < itemLen; i++){
                var itemObject = allItems[i];

                if ( my condition here)
                ){

//code below is not increasing the value for the current itemType in the amounts object. 
//Probably a simple syntax mistake?
                itemObject.itemType.increaseCount; 

                }

           }

           }

为什么我调用itemObject.itemType.increaseCount;不增加amount.itemType的值?

2 个答案:

答案 0 :(得分:1)

  

增加一个名为amountOfMegarockets的全局变量,但前提是该对象的itemType是“item_megarocket”。

不要为每个项目类型使用全局变量。请使用一个对象(在全局或本地范围内),该对象在其属性上计算每种类型的amouts。

var amounts = {},
    allItems = [];
function Item(itemName, itemType, itemPositionX, itemPositionY) {
    this.itemName = itemName;
    this.itemType = itemType;
    this.itemPositionX = itemPositionX;
    this.itemPositionY = itemPositionY;

    amounts[itemType]++ || (amounts[itemType]=1); // count by type
    allItems.push(this); // store all items
}

请注意,默认情况下我不会将所有Item个实例放在数组中,最好省略该行并让它执行调用者:

allItems.push(new Item('megarocket', 'item_megarocket', 108, 475));

答案 1 :(得分:0)

你可以做一些像

这样的事情
(function (global) {
    // create a scope
    var allItems = [];
    global.itemCreator = function(itemName, itemType, itemPositionX, itemPositionY) {
        this.itemName = itemName;
        this.itemType = itemType;
        this.itemPositionX = itemPositionX;
        this.itemPositionY = itemPositionY;
        allItems.push(this); //store all items in a global variable
        }//end itemCreator;
})(this);

这会奏效。但严重的是,不要让你的代码juste变得复杂,以使私有变量。如果有人想使用调试器作弊,他总会找到一种方法。

----编辑 如果您想要访问itemType上的全局变量,您可以执行以下操作:

function checkForItems(){ 
    var itemLen =allItems.length;
    for (i=0; i < itemLen; i++){
        var itemObject = allItems[i];

        if ( //checking for "collisions" here
            (ship.x < (itemObject.itemBitmap.x + itemObject.size) &&  (ship.x + shipWidth) > itemObject.itemBitmap.x) &&
            (ship.y < (itemObject.itemBitmap.y + itemObject.size) &&  (ship.y + shipWidth) > itemObject.itemBitmap.y)
        ){
            itemObject.actor.y = -500; //just removing the item from canvas here (temporary solution)
            // Here comes pseudo code for the part that I'm stuck with  
            if (window[itemObject.itemType + "Data"])) {
                variableBasedOnItemObject.itemType++;
            } else {
                window[itemObject.itemType + "Data"] = {
                    itemType: 1
                }
            }
        }
    }
}