Javascript:如何使用switch语句以正确的方式引用我的表达式?

时间:2016-08-11 10:59:41

标签: javascript

我写了一个小功能,需要4个技能按钮,并将它们置于我选择的位置。例如:

     function addSkillButton(name, position) {
            var x = document.createElement("button");
            var target = dom.el("buttonwrappers");
            x.textContent = name;
            x.setAttribute("id", name);
            x.setAttribute("class", "selection");
            target.insertBefore(x, target.children[position]);
     }
addSkillButton(swipe.name, 0);
addSkillButton(defend.name, 1);
addSkillButton(cure.name, 2);
addSkillButton(debuff.name, 3);

这样可以正常工作,但我心里想。随着游戏的进行,我将拥有50多种技能。而不是我手动输入'0-3'来在DOM上放置技能位置,我已经有一个skill object将每个技能放在一个类别类型中。我只想写一个switch语句,它将自动为我定位每个位置!所以我想出了这个:

 function addSkillButton(name) {
        var x = document.createElement("button");
        var target = dom.el("buttonwrappers");
        x.textContent = name;
        x.setAttribute("id", name);
        x.setAttribute("class", "selection");

        switch(skill.category){
            case "Attack": 
            target.insertBefore(x, target.children[0]);
            break;
            case "Defend": 
            target.insertBefore(x, target.children[1]);
            break;
            case "Healing": 
            target.insertBefore(x, target.children[2]);
            break;
            case "Debuff": 
            target.insertBefore(x, target.children[3]);
            break;
            default:
            console.log("Case Statement switched to default");
            console.log(skill.category);
        }
    }

我遇到的问题是skill.category是undefined。我在switch语句表达式中遗漏了什么吗?这是代码的其余部分。

技能对象:

skill = {
    bio: function (name, tier, category, description){
        this.name = name;
        this.tier = tier;
        this.category = category;
        this.description = description;
    },  
    list: [swipe, deepcut, balancedstrike, fury]
}

New = Object.create;

swipe = New(skill),
deepcut = New(skill),
balancedstrike = New(skill),
fury = New(skill),
defend = New(skill),
cure = New(skill),
debuff = New(skill);

swipe.bio("Swipe", 1, "Attack", "Basic Attack");
deepcut.bio("Deep Cut", 2, "Attack", "Poison your enemy - ignoring armor");
balancedstrike.bio("Balanced Strike", 2, "Attack", "Gain some of your balance back");
fury.bio("Fury", 2, "Attack", "Target loses balance");
defend.bio("Defend", 1, "Defend", "Maintain your balance");
cure.bio("Cure", 1, "Healing", "Heal your HP");
debuff.bio("Debuff", 1, "Debuff", "Lower enemys strength");

3 个答案:

答案 0 :(得分:0)

如果您在此函数object中传递addSkillButton,则必须在switch语句中调用this.skill.category,因为skill.category本身未定义。或者尝试使用name.skill.category

答案 1 :(得分:0)

您需要将整个对象传递给getAuth = function (req, res, next) { if(req.user) { db.getPerms({role_id: req.user.role_id, resource_id: req.resource.id}) .then(function(perms){ var allow = false; //you can do this mapping of methods to permissions before the db call and just get the specific permission you want. perms.forEach(function(perm){ if (req.method == "POST" && perms.create) allow = true; else if (req.method == "GET" && perms.read) allow = true; else if (req.method == "PUT" && perms.write) allow = true; else if (req.method == "DELETE" && perm.delete) allow = true; }) if (allow) next(); else res.status(403).send({error: 'access denied'}); })//handle your reject and catch here } else res.status(400).send({error: 'invalid token'}) } 而不是addSkillButton。 除此之外:

您可以使用装饰器设计模式将调用转发给另一个函数而不是另一个开关块。

使用以下模式:

name

然后:

 function addSkillButton(name) {
        var x = document.createElement("button");
        var target = dom.el("buttonwrappers");
        x.textContent = name;
        x.setAttribute("id", name);
        x.setAttribute("class", "selection");
        return addSkillButton[skill.category || '_default'](x,target);
    }
等等。 ...

答案 2 :(得分:0)

我明白了。而不是在调用它时在我的函数参数中使用swipe.name ..最好只使用整个对象本身。例如:

当时:

addSkillButton(swipe.name);

现在:

addSkillButton(swipe);

我现在引用整个对象,而不是它的一部分,所以现在我可以使用该对象来转发类别。

 function addSkillButton(object) {
        var x = document.createElement("button");
        var target = dom.el("buttonwrappers");
        x.textContent = object.name;
        x.setAttribute("id", object.name);
        x.setAttribute("class", "selection");

        switch(object.category){
            case "Attack": 
            target.insertBefore(x, target.children[0]);
            break;
            case "Defend": 
            target.insertBefore(x, target.children[1]);
            break;
            case "Healing": 
            target.insertBefore(x, target.children[2]);
            break;
            case "Debuff": 
            target.insertBefore(x, target.children[3]);
            break;
            default:
            console.log("Case Statement switched to default");
            console.log(object.name);
        }
    }