1 回答
TA贡献1895条经验 获得超3个赞
假设它们可以提前定义,为了拥有像 的子属性pineapple.is_a.fruit,您需要在对象的is_a和is属性上定义对象。例如(见评论):
class Item { // `Item` rather than `Thing`, right?
constructor(type) {
this.type = type;
// Create an `is_a` property that's an object with a `fruit` property
this.is_a = {
fruit: false // Or whatever the initial value should be
};
// Create an `is` property that's an object with a `heavy` property
this.is = {
heavy: false // Or whatever the initial value should be
};
}
}
const pineapple = new Item('pineapple');
pineapple.type = "fruit"; // I added quotes here
console.log("is_a.fruit before:", pineapple.is_a.fruit);
console.log("is.heavy before:", pineapple.is_a.fruit);
pineapple.is_a.fruit = true;
pineapple.is.heavy = true;
console.log("is_a.fruit after: ", pineapple.is_a.fruit);
console.log("is.heavy after: ", pineapple.is_a.fruit);
添加回答
举报