Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数,该函数使用构造函数创建对象,并使用javascript返回已完成的对象_Javascript_Jquery - Fatal编程技术网

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

函数,该函数使用构造函数创建对象,并使用javascript返回已完成的对象,javascript,jquery,Javascript,Jquery,我正在使用对象构造函数创建对象。我正在嵌套这些对象,如: Object = { property: { property123: "test", property321: true, }, property2: { name: "Mike", age: 20, } }; 为了向您提供更多详细信息,您可以在JSFIDLE上查看我的代码: 正如你所看到的,我创造了一些像“武器掌握”、“剑”、“斧”、“武

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

Object = {
    property: {
        property123: "test",
        property321: true,
    },
    property2: {
        name: "Mike",
        age: 20,
    }
};
为了向您提供更多详细信息,您可以在JSFIDLE上查看我的代码:

正如你所看到的,我创造了一些像“武器掌握”、“剑”、“斧”、“武器技能类型”这样的东西

我想要的是一个函数,它将返回“武器掌握”,其中存储(嵌套)了所有这些对象

这样其他对象就不会是全局对象,因为我不使用它们。我只是在使用“掌握武器”

目前所有的物体都是全球性的,我只希望“武器掌握”是全球性的,任何其他的都是不必要的。如果有更好的方法来创建我的对象,请告诉我


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

我发表了评论,因为我认为您真正需要的是模块。但下面是如何完成您的要求:在JavaScript中变量的作用域是函数。因此,将所有变量声明封装在一个函数中,只将
武器掌握
作为全局变量公开。您可以使用匿名函数作为包装器。看起来是这样的:

(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:

我理解你的问题的方式是,你想要更清晰地组织事情,以避免全局性。在我看来,您寻求的是模块化JavaScript。使用CommonJS(或者模块化JS的另一种方法),您可以避免全局性。。。在…中,我同意他的观点——虽然这是一个单独的问题,但你应该在这里或谷歌上搜索该主题。您好,谢谢您的回答以及关于“模块化JavaScript”的其他信息,我也将研究这些主题。我还搜索了“for in”,发现在数组上使用它是不好的,而不是在对象属性上使用它。如果你能给我更多关于这方面的信息,我会非常感激:)同时我会继续寻找这个话题。@Mariusz我不认为这有什么大不了的,如果你最终碰到了一些bug,这只是需要注意的事情。我更喜欢做
Object.keys(myObject.forEach(function(key){…})但是ES6有一些更好的方法。别担心!嘿,再次谢谢你。我不知道ES6是什么,但我会读到它。我也会记住你这样做的方式,以备将来参考。谢谢:)