基于对象属性修改全局变量
Modify a global variable based on an object property
我正在尝试学习面向对象的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的调用;不增加amounts.itemType的值?
递增一个名为 amountOfMegarockets 的全局变量,但前提是该对象的 itemType 为 "item_megarocket"。
不要对每种项类型使用全局变量。使用一个对象(在全局或局部范围内(,该对象在其属性上计算每种类型的 amout。
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));
你可以做一些事情,比如
(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);
这将起作用。 但说真的,不要太复杂化你的代码,只是为了制作私有变量。 如果有人想使用调试器作弊,他总会找到一种方法来做到这一点。
----编辑如果你只是想访问基于itemType的全局var,你可以做一些事情,比如:
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
}
}
}
}
}
- 如何通过JQuery修改样式属性
- 在运行时修改innerhtml属性
- 通过emberJS中的控制器修改组件的属性
- HTML&JS-如何修改CSS类的属性
- js添加封装来修改属性值
- 检查所有元素的属性并修改另一个元素
- 选择单选输入时修改占位符属性
- Lodash搜索对象属性并修改值
- 复选框的checked属性的修改不会;工作不正常
- JavaScript-我可以用for循环中修改的属性将新结构推送到数组中吗
- 修改时,哪些DOM元素属性会导致浏览器执行回流操作
- 有没有一种方法可以在一次调用中修改每个jquery对象的特定属性
- 从 React 中的自定义元素中修改属性
- AngularJS:修改指令中的transcluded属性键
- 在 Backbone 中侦听和修改模型属性的正确方法是什么.js
- 在 for 循环内分组并根据样式属性修改 span 元素
- 基于对象属性修改全局变量
- 使用Jquery遍历DOM以查找输入并根据数据属性修改值
- Javascript特定属性修改
- 加快基于属性修改表单元格范围(行/列选择)的速度