2 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是一個(gè)方法:
let House = function(city, postalCode, street, streetNumber){
this.city = city;
this.postalCode = postalCode;
this.street = street;
this.streetNumber = streetNumber;
return {
getAddress: function() {
return {
'address': {
'city': city,
'postalCode': postalCode,
'street': street,
'streetNumber': streetNumber
}
};
}
};
};
let Person = function(name, surname, yearOfBirth, monthOfBirth, dayOfBirth, bornCity, employment, house, car){
this.name = name;
this.surname = surname;
this.yearOfBirth = yearOfBirth;
this.monthOfBirth = monthOfBirth;
this.dayOfBirth = dayOfBirth;
this.bornCity = bornCity;
this.employment = employment;
this.house = house;
this.car = car;
return {
getFullPerson: function() {
return {
'personalInformation': {
'name': name,
'surname': surname,
'dateOfBirth': {
'year': yearOfBirth,
'month': monthOfBirth,
'day': dayOfBirth
},
'bornCity': bornCity
},
'employment': employment,
'home': (house ? house.getAddress() : null), // pseudocode
'car': (car ? car.getCar() : null) // pseudocode
};
}
};
};
let house1 = new House('city', 'postalCode', 'street', 'streetNumber');
let person1 = new Person('person', '1', 2020, 1, 1, 'bornCity', 'employment', null, null);
let person2 = new Person('person', '2', 2020, 1, 1, 'bornCity', 'employment', house1, null);
console.log(person1.getFullPerson());
console.log(person2.getFullPerson());
創(chuàng)建您想要與 關(guān)聯(lián)的任何House或?qū)ο?。然后,您可以選擇在創(chuàng)建每個(gè) 時(shí)傳入或?qū)ο蟆C總€(gè)或?qū)ο笠矊⒃谠搶ο髢?nèi)具有可用的功能。CarPersonhomecarPersonhomecarPerson

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
據(jù)我所知,您希望在 Person 對象中引用一個(gè)人的房屋和汽車。因此,要使其工作,您唯一需要做的就是將 House 和 Car 對象傳遞給 Person:
let Person = function (
name,
surname,
yearOfBirth,
monthOfBirth,
dayOfBirth,
bornCity,
employment,
home,
car
) {
this.name = name
this.surname = surname
this.yearOfBirth = yearOfBirth
this.monthOfBirth = monthOfBirth
this.dayOfBirth = dayOfBirth
this.bornCity = bornCity
this.employment = employment
this.home = home
this.car = car
return {
getFullPerson: function () {
return {
personalInformation: {
name: name,
surname: surname,
dateOfBirth: {
year: yearOfBirth,
month: monthOfBirth,
day: dayOfBirth,
},
bornCity: bornCity,
},
employment: employment,
home: home.getAddress(),
car: car.getCar(),
}
},
}
}
添加回答
舉報(bào)