我有一个RPG类型字符表的设备插槽列表。每个插槽的类型都类似于“头”或“身体”,并且每个插槽都有一个代表角色所穿物品的物体。每台设备将具有相同的变量类型(如下所示),但我不想单独编写每台设备,因为列表很长。有没有办法说“每种类型的设备都会有这些变量”。下面是我的尝试,但没有用(说“ head is undefined”),或者如果我移除了head(“ body undefined”)等。
equipment: {
head,
body,
legs,
feet,
shoulders,
arms,
hands,
back,
ringLeft,
ringRight,
neck,
rightHand,
leftHand,
ammunition: {
default: {
name: {
type: String,
default: "Empty"
},
Type: {
type: String,
default: ""
},
subType: {
type: String,
default: ""
},
handling: {
type: String,
default: ""
},
rarity: {
type: String,
default: ""
},
durability: {
max: {
type: Number,
default: 0
},
current: {
type: Number,
default: 0
}
},
damage: {
min: {
type: Number,
default: 0
},
max: {
type: Number,
default: 0
}
},
armorClass: {
type: Number,
default: 0
},
str: {
type: Number,
default: 0
},
dex: {
type: Number,
default: 0
},
int: {
type: Number,
default: 0
},
accuracy: {
type: Number,
default: 0
},
magicAccuracy: {
type: Number,
default: 0
},
magicDefense: {
type: Number,
default: 0
},
armorPiercing: {
type: Number,
default: 0
},
resistances: {
fire: {
type: Number,
default: 0
},
ice: {
type: Number,
default: 0
},
lightning: {
type: Number,
default: 0
},
water: {
type: Number,
default: 0
},
earth: {
type: Number,
default: 0
},
wind: {
type: Number,
default: 0
},
light: {
type: Number,
default: 0
},
dark: {
type: Number,
default: 0
}
}
}
}
}
答案 0 :(得分:0)
是的,您可以创建一个子模式。这只是正常的猫鼬模式,但您可以使用该架构声明属性,因此不必再次键入所有内容。 测试 因此,..创建一个新的架构:
const schema_equipmentAttributes = new Schema({
name: {
type: String,
default: "Empty"
},
Type: {
type: String,
default: ""
},
subType: {
type: String,
default: ""
}
});
然后您可以声明:
equipment: {
head: { type: schema_equipmentAttributes },
body: { type: schema_equipmentAttributes },
legs: { type: schema_equipmentAttributes },
feet: { type: schema_equipmentAttributes }
}
这应该是最简单的方法:)