" '未定义'不是一个功能"错误

时间:2014-06-12 19:19:03

标签: javascript oop object constructor

所以,我想编写用随机统计数据创建单位的代码。

这是Enemy Constructor:

  function Enemy(name, atk, def, hp, mp, xp_val){
    this.name = name;
    this.atk = atk;
    this.def= def;
    this.hp = hp;
    this.max_hp = hp;
    this.mp = mp;
    this.max_mp = mp;
    this.xp_val = xp_val;
    totalEnemies++; 
}

这是创建随机敌人的补充代码。

function createEnemy(name, difficulty){
    var atk = getIntBetween(5, 10);
    var def = getIntBetween (0, 5);
    var hp = getIntBetween(15, 25);
    var mp = getIntBetween(0, 7);
    var xp_val = getIntBetween(5, 10);
    var multiplier;

    if (difficulty === 1 || difficulty.toLowerCase() === "easy"){ multiplier = 1; }
    if (difficulty === 2 || difficulty.toLowerCase() === "average"){ multiplier = 2; }
    if (difficulty === 3 || difficulty.toLowerCase() === "challenge"){ multiplier = 4; }
    if (difficulty === 4 || difficulty.toLowerCase() === "damn"){ multiplier = 7; }
    if (difficulty === 5 || difficulty.toLowerCase() === "nightmare"){ multiplier = 11; }

    atk *= multiplier;
    def *=multiplier;
    hp *= multiplier;
    mp *= multiplier;
    xp_val *= multiplier;

    return new Enemy(name, atk, def, hp, mp, xp_val);
}

我尝试用:

创建一个实例
var goblin_02 = createEnemy("Goblin Dirtbag", 2);

我在标题中收到错误。这一切都是为了规避JS'缺乏重载的构造函数,我哪里出错?

2 个答案:

答案 0 :(得分:1)

我想它就在这里:

if (difficulty === 1 || difficulty.toLowerCase() === "easy"){ multiplier = 1; }

您正在为2传递difficulty。它不等于1,因此您接下来基本上会调用2.toLowerCase()。不出所料,这不是真的。

答案 1 :(得分:0)

我在代码中做了一些更改,但它已经有效了。

<script>
        var totalEnemies = 0;
        function Enemy(name, atk, def, hp, mp, xp_val){
            this.name = name;
            this.atk = atk;
            this.def= def;
            this.hp = hp;
            this.max_hp = hp;
            this.mp = mp;
            this.max_mp = mp;
            this.xp_val = xp_val;
            totalEnemies++; 
        }
        function createEnemy(name, difficulty){
            var atk = getIntBetween(5, 10);
            var def = getIntBetween (0, 5);
            var hp = getIntBetween(15, 25);
            var mp = getIntBetween(0, 7);
            var xp_val = getIntBetween(5, 10);
            var multiplier;

            if (((typeof(difficulty) === "number") &&  (difficulty === 1)) || ((typeof(difficulty) === "string") &&  (difficulty.toLowerCase() === "easy"))){ multiplier = 1; }
            if (difficulty === 2){ multiplier = 2; }
            if (difficulty === 3){ multiplier = 4; }
            if (difficulty === 4){ multiplier = 7; }
            if (difficulty === 5){ multiplier = 11; }

            atk *= multiplier;
            def *=multiplier;
            hp *= multiplier;
            mp *= multiplier;
            xp_val *= multiplier;

            return new Enemy(name, atk, def, hp, mp, xp_val);
        }
        function create() {
            var goblin_02 = createEnemy("Goblin Dirtbag", 2);
            console.log(goblin_02,totalEnemies);
        }
        function getIntBetween(i,j){
            var number = Math.floor((Math.random() * j) + i);
            return number;
        }
    </script>
</head>
<body onload="create()">

</body>

您只需要更改所有if块的if条件。

相关问题