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);
添加回答
舉報