简体   繁体   English

带有数组或子对象的Javascript OOP构造函数?

[英]Javascript OOP Constructor with array or sub-object?

I am new to true OOP but I understand javascript pretty alright. 我是真正的OOP的新手,但我理解javascript还不错。 I'm trying to make new objects using a constructor pattern but I would like to make one of the properties an array. 我正在尝试使用构造函数模式创建新对象,但我想将其中一个属性设置为数组。

Here's an example of what I'm trying to do: 这是我要执行的操作的一个示例:

function Walker(name, type, ws, bs, s, armor, i, a, hp) {
    this.name = name;
    this.type = type;
    this.ws = ws;
    this.bs = bs;
    this.s = s;
    this.armor = new Array("f", "s", "r");
    this.i = i;
    this.a = a;
    this.hp = hp;
}

This is for a game some of you might know (and only for personal use, as the company who created the game has a stick up their... for their IP) 这是一些您可能知道的游戏(并且仅供个人使用,因为创建游戏的公司对自己的IP坚持不懈...)

As you can see, the armor property is going to have 3 properties inside of it. 如您所见, armor属性将在其中包含3个属性。 The reason I'm doing it this way is because there is already a property named s , so I don't want that property and the armor property of s to be mixed up. 我这样做的原因是因为已经有一个名为s的属性,所以我不希望将该属性与s的盔甲属性混合使用。

I am making a new walker and trying to log the armor like below: 我正在制作一个新的walker并尝试记录如下装甲:

var specificWalker = new Walker("Specific Walker", "Vehicle", 5, 5, 6, [12, 12, 10], 4, 2, 3);

console.log(specificWalker.armor[0]);

Though, of course this isn't working because armor[0] is always equal to "f" and I don't know how to override that that part of the array. 但是,这当然是行不通的,因为armor[0]始终等于“ f”,而且我不知道如何覆盖数组的那部分。

Ideally, what I would like to do is be able to log the armor this way: 理想情况下,我想做的就是能够这样记录装甲:

console.log(specificWalker.armor.f) //Should log "12"

But I'm unsure on how to make an object inside of an object. 但是我不确定如何在对象内部创建对象。

Can anyone help on this one? 有人可以帮忙吗?

You just need to create an Object, instead of an Array, like this 您只需要创建一个对象,而不是一个数组,就像这样

this.armor = ["f", "s", "r"].reduce(function(result, current, index) {
    result[current] = armor[index];
    return result;
}, {});

Now, this.armor is not an Array, but an Object. 现在, this.armor不是数组,而是对象。 When you print specificWalker.armor , you will get something like this 当您打印specificWalker.armor ,您将获得类似以下内容

{ f: 12, s: 12, r: 10 }

and then you can access f , like you wanted 然后您可以按需要访问f

console.log(specificWalker.armor.f);
// 12

i think you have so many arguments on the constructor, i'd do it this way: 我认为您在构造函数上有这么多参数,我会这样做:

function Walker(prop) {
    this.name = prop.name;
    this.type = prop.type;
    this.ws = prop.ws;
    this.bs = prop.bs;
    this.s = prop.s;
    this.armor = prop.armor;
    this.i = prop.i;
    this.a = prop.a;
    this.hp = prop.hp;
}

and call it this way 并这样称呼它

var walker = new Walker({
    name : "",
    type : "",
    ws : "",
    bs : "",
    s : "",
    armor : [12,12,39],
    i : "",
    a : "",
    hp : ""
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM