将对象内部的对象读取为未定义

时间:2018-06-17 23:49:46

标签: javascript class object undefined

我正在尝试制作一个口袋妖怪风格的游戏,你孵化和进化龙。大多数龙都有一条更强大的龙,它们会进化。

https://jsbin.com/pulawoz/15/edit?html,js,output

当我创建“evolve”功能时,我遇到了麻烦。它总是将正在演化的龙的evolvesInto属性(dragonToEvolve)读为未定义。

您可以在此处查看问题代码:

var dragons = [];
var enemyDragon;

function Dragon(name, ATK, maxATK, HP, maxHP, normalPowers, continuousPowers,
    responsePowers, type, cry, evolvesInto, numberOfAttacks) {

    this.name = name;
    this.ATK = ATK;
    this.HP = HP;
    this.maxATK = maxATK;
    this.maxHP = maxHP;
    this.normalPowers = normalPowers;
    this.continuousPowers = continuousPowers;
    this.responsePowers = responsePowers;     
    this.type = type;        
    this.cry = cry;        
    this.evolvesInto = evolvesInto;        
    this.numberOfAttacks = numberOfAttacks        
}

// Dragons

var lusterDragon = new Dragon("Luster Dragon", 1900, 1900, 2500, 2500, [],
   [], [], "Wind", "Shining Emerald Blast! Sha-shing!", lusterDragon2, 1);

var lusterDragon2 = new Dragon("Luster Dragon 2", 2400, 2400, 3100, 3100, [], 
    [], [], "Wind", "Ultimate Emerald Blast! Shaka-shing!", null, 1);

var wattailDragon = new Dragon("Wattail Dragon", 2500, 2500, 3000, 3000, [], 
    [], [], "Light", "Shock Bolt! KRAKA-THOOM!", null, 1);

var babyDragon = new Dragon("Baby Dragon", 1200, 1200, 1800, 1800, [], [], 
    [], "Wind", "Mini Fang! Chomp!", thousandDragon, 1);

var thousandDragon = new Dragon("Thousand Dragon", 2400, 2400, 3500, 3500, 
    [], [], [], "Wind", "Thousand Fang! Chomp!", null, 1);

var alexandriteDragon = new Dragon("Alexandrite Dragon", 2000, 2000, 2400, 
    2400, [], [], [], "Light", "Diamond Blast! Sha-shing!", null, 1);

var hyozanryu = new Dragon("Hyozanryu", 2100, 2100, 3800, 3800, [], [], [], 
    "Light", "Diamond Blade! Shing!", lightEndDragon, 1);

var labradoriteDragon = new Dragon("Labradorite Dragon", 0, 0, 4500, 4500, 
    [], [], [], "Dark", "...", darkEndDragon, 1);

var darkEndDragon = new Dragon("Dark End Dragon", 2600, 2600, 4000, 4000, 
    [darkDrain], [], [], "Dark", "Infernal Force! OOOOOOOOOM!", null, 1);

var lightEndDragon = new Dragon("Light End Dragon", 2600, 2600, 4000, 
    4000[lightBlast], [], [], "Light", "Shining Force! OOOOOOOOM!", null, 1)

var cyberDragon = new Dragon("Cyber Dragon", 2100, 2100, 2700, 2700, [], [], 
    [], "Light", "Cyber Burst! FWOOOOM!", cyberTwinDragon, 1);

var cyberTwinDragon = new Dragon("Cyber Twin Dragon", 2800, 2800, 3600, 3600, 
    [], [], [], "Light", "Cyber Burst v2! FWOOOOM!", cyberEndDragon, 2);

var cyberEndDragon = new Dragon("Cyber End Dragon", 4000, 4000, 5000, 5000, 
    [cyberPierce], [], [], "Light", "Ultimate Cyber Burst! FWOOOOOOOOOOOOOOOOOOOOM!", 
    null, 1)

var cyberDragonDrei = new Dragon("Cyber Dragon Drei", 1800, 1800, 2600, 2600, 
    [], [], [], "Light", "Cyber Burst! FWOOM!", cyberDragonNova, 1);

var cyberDragonNova = new Dragon("Cyber Dragon Nova", 2100, 2100, 3000, 3000, 
    [cyberUpgrade], [], [], "Light", "Helios Cyber Burst! FWOOOM!",
    cyberDragonInfinity, 1);

var cyberDragonInfinity = new Dragon("Cyber Dragon Infinity", 2100, 2100, 
    3000, 3000, [cyberAbsorb], [cyberGain], [cyberCancel], "Light", 
    "Infinite Cyber Burst! FWOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOM!", 
    null, 1);

var evolveAsk = function(){
    var selectedDragon = prompt("Which dragon do you want to evolve?");
    var dragonToEvolve = selectedDragons[selectedDragon-1];

    console.log(dragonToEvolve.name + " selected.");

    dragons.shift(dragonToEvolve);

    console.log(dragonToEvolve);

    dragons.push(dragonToEvolve.evolvesInto);            
};

var evolvePrint = function(){
    console.log(selectedDragons);          
    var i = 0;

    for (i = 0; i < dragons.length; i++) { 
        if(dragons[i].evolvesInto !== null){
            console.log("Press " + (i+1) + " to select " + dragons[i].name);
            selectedDragons.push(dragons[i]);
        }
    }          
};

var selectedDragons = [];

var evolve = function(){          
    selectedDragons = [];
    evolvePrint();

    setTimeout(function(){
        evolveAsk();
    },1000);
}

我是一个菜鸟,所以可能会有一些明显我不知道的东西。非常感谢任何帮助!

P.S。 如果要测试它,请输入以下命令:

dragons.push(lusterDragon);
evolve();

然后按1并输入。

2 个答案:

答案 0 :(得分:1)

您的evolvesInto财产中有一些令人困惑的事情。对于lusterDragon,它是一个数组,对于其他龙,它是一个变量。

我们需要使这一点保持一致。将所有变量转换为字符串的最简单的解决方案:

var lusterDragon = new Dragon("Luster Dragon", 1900, 1900, 2500, 2500, [], [], [], "Wind", "Shining Emerald Blast! Sha-shing!", 'lusterDragon2', 1);

如果你想把它保存为嵌套对象(我认为你想要),你首先需要定义lusterDragon2,例如Fiddle:https://jsbin.com/nevaqicaro/1/edit

就像现在一样,它尚未定义,因为尚未定义变量lusterDragon2

答案 1 :(得分:1)

绝对有一种方法可以使用像@Joseph Cho那样的字符串来表示你可以使用一个处理程序来选择evolvable类型,或者你可以从另一个类扩展该类。

@Rudolf Manusachi说的是什么,我认为第一个问题是你的进化类型没有在你未解决的类型之前定义,因此不能传递给你的对象构造函数

检查 - &gt; example jsbin

// Dragons
// luster 2 is now first ->
// to not have undefined they need to be defined before you may use them
// should probly happen with all evolvable dragon types 
var lusterDragon2 = new Dragon("Luster Dragon 2", 2400, 2400, 3100, 3100, [], [], [], "Wind", "Ultimate Emerald Blast! Shaka-shing!", null, 1);
var lightEndDragon = new Dragon("Light End Dragon", 2600, 2600, 4000, 4000, [lightBlast], [], [], "Light", "Shining Force! OOOOOOOOM!", null, 1);
var thousandDragon = new Dragon("Thousand Dragon", 2400, 2400, 3500, 3500, [], [], [], "Wind", "Thousand Fang! Chomp!", null, 1);

var lusterDragon = new Dragon("Luster Dragon", 1900, 1900, 2500, 2500, [], [], [], "Wind", "Shining Emerald Blast! Sha-shing!", [lusterDragon2], 1);

这是对象继承的完美用例,或者如果你感觉到它,es6类和扩展,它们有点相同:Obj Constructors/ Inheritance