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

Function which create an object using a constructor and return completed object using javascript

本文关键字:函数 已完成 对象 返回 创建对象 构造函数 javascript      更新时间:2023-09-26

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

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

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

正如你所看到的,我几乎没有创建过像"weaponMastery"、"剑"、"斧"、"weaponSkillType"这样的对象。

我想要的是一个函数,它将返回"weaponMastery",其中存储(嵌套)了所有这些对象

这样其他对象就不会是全局的,因为我不使用它们。我只是在使用"武器大师"。

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

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

我评论道,我认为您真正想要的是模块。但以下是如何实现您的要求:变量的作用域是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/