2

I'm learning JavaScript, I've created three different object with some attributes: Person, House and Car.

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){

  this.name = name;
  this.surname = surname;
  this.yearOfBirth = yearOfBirth;
  this.monthOfBirth = monthOfBirth;
  this.dayOfBirth = dayOfBirth;
  this.bornCity = bornCity;
  this.employment = employment;

  return {

    getFullPerson: function() {

      return {
          'personalInformation': {
            'name': name,
            'surname': surname,
            'dateOfBirth': {
              'year': yearOfBirth,
              'month': monthOfBirth,
              'day': dayOfBirth
            },
            'bornCity': bornCity
          },
          'employment': employment,
          'home': getAddress(), // pseudocode
          'car': getCar() // pseudocode
      };

    }

  };

};

I would like to create a link between this objects because I need to call, for example, who person lives inside an house and viceversa; but I've no clear how I can do this.

MaxDragonheart
  • 1,117
  • 13
  • 34
  • 1
    you can have an attribute `house` inside `person`, or a common key between these two – BhaskerYadav Nov 06 '20 at 08:41
  • https://www.w3resource.com/java-tutorial/inheritance-composition-relationship.php | [HAS-A, IS-A terminology in object oriented language](https://stackoverflow.com/q/2218937) | – VLAZ Nov 06 '20 at 08:44

2 Answers2

1

As I see you want to have a reference to a person's home and car in the Person object. So the only thing you need to do to make it work is to pass a House and a Car object to the 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(),
      }
    },
  }
}
1

Here's a way:

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());

Create any House or Car objects that you want to associate with a Person. Then you can optionally pass in the home or car objects when creating each Person. Each home or car object will have its functions available within the Person object too.

sbgib
  • 5,580
  • 3
  • 19
  • 26
  • 1
    Thank you! One question: this `(house ? house.getAddress() : null)` is an `if else` statement. Right? Sounds like this: `if there is house get house, else null` – MaxDragonheart Nov 06 '20 at 09:19
  • 1
    That's exactly right, it's just a shorter way of doing that, called a [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator). It avoids an error if the `Person` object does not have a `House`/`Car` associated with them. – sbgib Nov 06 '20 at 09:23