简体   繁体   English

在JavaScript中添加对象属性

[英]Adding object properties in JavaScript

I am trying to subtract the value of object1's objectProperty from object2's object property and I keep getting NaN on the console. 我试图从object2的object属性中减去object1的objectProperty的值,并且在控制台上一直得到NaN。 Here is example code: 这是示例代码:

    object1.objectProperty - object2.object2Property

If this isn't enough to go off, I can post the full code from my project. 如果还不够,我可以从项目中发布完整的代码。 If there is another way to do this or some kind of function that can help, please let me know. 如果还有另一种方法可以执行此操作,或者某种可以帮助您的功能,请告诉我。

edit: Here is the code.. 编辑:这是代码。

    var myPokemon = {
    health: 25,
    defense: 5,
    attack: 10,
    speed: 5
};

var moves = {
    Scratch: 5,
    Bite: 5,
    Slap: 5,
    Growl: 1
};


var computerPokemon = {
    health: 20,
    defense: 5,
    attack: 10, 
    speed: 7
};

function calcDamage(firstPokemon, secondPokemon, move) {
    if(move == moves.Growl){
        //starts here
        var newDefense =  moves.Growl - firstPokemon.defense;
        console.log(newDefense);
        //ends here
    }else{
    var newHealth = (firstPokemon.health + firstPokemon.defense) - (secondPokemon.attack + move);
    console.log(newHealth);
    }
}

edit: When I did moves.Growl - firstPokemon.defense || 0; 编辑:当我做了moves.Growl - firstPokemon.defense || 0; moves.Growl - firstPokemon.defense || 0; it returned -4 instead of NaN which is what I wanted it to do, but the person that answered that removed the answer so this has been answered by whoever that guy was. 它返回了-4,而不是我想要的NaN,但是回答的人删除了答案,所以无论那个人是谁,它都回答了。

Use parseInt to convert the values into integer and then do the math. 使用parseInt将值转换为整数,然后进行数学运算。

 var value = parseInt(object1.objectProperty,10) - parseInt(object2.object2Property,10);

The Problem is here 问题在这里

var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));

The last one Number(move.Growl) it should be Number(moves.Growl) moves not move . 最后一个Number(move.Growl)应该是Number(moves.Growl) movesmove In your case move is just a number and you are trying Number(move.Growl) which will be NaN and hence your getting NaN. 在您的情况下, move只是一个数字,而您正在尝试Number(move.Growl) ,它将为NaN,因此得到NaN。

The problem is that you are adding the object in the second argument. 问题是您要在第二个参数中添加对象。 Also your if statement will never execute, I have fixed both as following 另外,您的if语句将永远不会执行,我已修复了以下两个问题

  var myPokemon = { health: 25, defense: 5, attack: 10, speed: 5 }; var moves = { Scratch: 5, Bite: 5, Slap: 5, Growl: 1 }; var computerPokemon = { health: 20, defense: 5, attack: 10, speed: 7 }; function calcDamage(firstPokemon, secondPokemon, move) { if(moves.Growl!=undefined){ //starts here var newDefense = moves.Growl - firstPokemon.defense; alert(newDefense); //ends here }else{ var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl)); alert(newHealth); } } calcDamage(myPokemon,computerPokemon,moves) 

Usually, if you are getting NaN, you are probably working with other elements but numbers. 通常,如果您获得的是NaN,则可能正在处理除数字以外的其他元素。 Are you sure they both are numbers? 您确定它们都是数字吗?

Just an example: 只是一个例子:

var x = {}, y = {};
x.r = 10;
y.r = 5;
x.r - y.r; // yields 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM