使用构造函数创建对象并使用javascript返回完成对象的函数

时间:2015-06-27 07:28:58

标签: javascript jquery

我正在使用对象构造函数创建对象。我正在嵌套这些对象,如:

Object = {
    property: {
        property123: "test",
        property321: true,
    },
    property2: {
        name: "Mike",
        age: 20,
    }
};

为了给你更多详细信息,你可以在jsfiddle上查看我的代码:http://jsfiddle.net/8v2b9x7p/3/

正如你所看到的,我创造了很少的对象,如" weaponMastery"," sword"," ax"," weaponSkillType"。

我想要的是一个能够返回" weaponMastery"它将所有这些对象存储(嵌套)在其中。

这样,由于我不使用它们,其他对象不会是全球性的。我只使用" weaponMastery"。

目前所有对象都是全局的,我想要的只是" weaponMastery"要是全球性的,任何其他都没有必要。如果有更好的方法来创建我的对象,请告诉我。

我正在研究删除它们的方法,但我也没有找到任何好的解决方案。感谢任何帮助,谢谢:)

1 个答案:

答案 0 :(得分:2)

我评论说,我认为你真正想要的是模块。但是这里是你如何能够完成你的要求:变量的范围是JavaScript中的函数。因此,将所有变量声明包装在函数中,并仅将weaponMastery公开为全局变量。您可以使用匿名函数作为包装器。它看起来像这样:

(function() {
  ...
})();

使用var在内部声明的任何变量只能在该函数中作用域。如果要将变量公开给全局范围,可以在其前面添加window.,以便说出window.weaponMastery

以下是您使用该方法的代码:

(function() {
    var weaponSkillType = function (level, experience, maxExperience, image, name) {
        this.level = level;
        this.experience = experience;
        this.maxExperience = maxExperience;
        this.image = image;
        this.name = name;
    };
    var sword = new weaponSkillType(0, 0, 10, "sword", "Sword");
    var axe = new weaponSkillType(0, 0, 10, "axe", "Axe");
    var mace = new weaponSkillType(0, 0, 10, "mace", "Mace");
    var staff = new weaponSkillType(0, 0, 10, "staff", "Staff");
    var ranged = new weaponSkillType(0, 0, 10, "ranged", "Ranged");
    var fist = new weaponSkillType(0, 0, 10, "fist", "Fist");

    sword.strength = function () {
        return this.level * 2;
    };
    sword.swordStrength = function () {
        return player.isSword ? this.strength() : 0;
    };
    sword.agility = function () {
        return this.level * 1.5;
    };
    sword.swordAgility = function () {
        return player.isSword ? this.agility() : 0
    };

    axe.strength = function () {
        return this.level * 2;
    };
    axe.axeStrength = function () {
        return player.isAxe ? this.strength() : 0;
    };
    axe.endurance = function () {
        return this.level * 1.5;
    };
    axe.axeEndurance = function () {
        return player.isAxe ? this.endurance() : 0;
    };

    mace.endurance = function () {
        return this.level * 2;
    };
    mace.maceEndurance = function () {
        return player.isMace ? this.endurance() : 0;
    };
    mace.wisdom = function () {
        return this.level * 1.5;
    };
    mace.maceWisdom = function () {
        return player.isMace ? this.wisdom() : 0;
    };

    staff.intelligence = function () {
        return this.level * 2;
    };
    staff.staffIntelligence = function () {
        return player.isStaff ? this.intelligence() : 0;
    };
    staff.wisdom = function () {
        return this.level * 1.5;
    };
    staff.staffWisdom = function () {
        return player.isStaff ? this.wisdom() : 0;
    };

    ranged.strength = function () {
        return this.level * 1.5;
    };
    ranged.rangedStrength = function () {
        return player.isRanged ? this.strength() : 0;
    };
    ranged.dexterity = function () {
        return this.level * 2;
    };
    ranged.rangedDexterity = function () {
        return player.isRanged ? this.dexterity() : 0;
    };

    fist.agility = function () {
        return this.level * 1.5;
    };
    fist.fistAgility = function () {
        return player.isFist ? this.agility() : 0;
    };
    fist.dexterity = function () {
        return this.level * 2;
    };
    fist.fistDexterity = function () {
        return player.isFist ? this.dexterity() : 0;
    };

    window.weaponMastery = new Object();
    weaponMastery.sword = sword;
    weaponMastery.axe = axe;
    weaponMastery.mace = mace;
    weaponMastery.staff = staff;
    weaponMastery.ranged = ranged;
    weaponMastery.fist = fist;
})();


function test() {
    var html = '';
    for (weapon in weaponMastery) {
        html += weapon + ', ';
    }
    document.getElementById('test').innerHTML = html;
};
test();

JSFiddle:http://jsfiddle.net/nxtdhtts/