Aggregated Properties

Discusses the purpose and use of aggregated properties

Background

As you have learned in Classes and Instances, it is possible to both define a class, and then extend the class to create a new class. This is our implementation of inheritance from object-oriented programming. When you extend an existing class, however, the extended class overwrites the values of the base class. This happens for both data properties and methods.

When a method overwrites the method from its base class, you can use the _super method to pass execution to the base class. But, when a data property overwrites its corresponding data property on the base class, the data property on the base class is not easily accessible. What about when you do not want to overwrite the data property on the base class, but extend it—similar to how we extend class?

By default, data properties from an extended class hide the data properties on its base class, making them hard to access.

This is where aggregated properties come into the picture. The purpose of an aggregated property is to combine the property of the base class with that of the extended class. This allows the data properties to be extended in a similar manner to how a class definition is extended.

Supported Types

Our object model supports the following types of aggregated properties:

Concatenated Properties

Concatenated properties apply to array data properties. You use the concatProperties property on the class definition to define the list of properties that should be concatenated instead of overwritten.

const Person = BO.extend ({
  concatProperties: ['greetings'],  // Define the concatenated properties
  greetings: ['Hello']
});

const Student = Person.extend ({
  greetings: ['YOLO']
});

let person = new Person ();
console.log (person.greetings);     // ["Hello"]

let student = new Student ();
console.log (student.greetings);    // ["Hello", "YOLO"]

The concatenated properties also apply when initializing a created object.

Merged Properties

Merged properties apply to plain JavaScript object data properties. You use the mergedProperties property on the class definition to define the list of properties that should be merged instead of overwritten.

const Person = BO.extend ({
  mergedProperties: ['capabilties'],      // Define the merged properties
  
  capabilties: {
    a: { a: 1, b: 3}
    b: { a: 6 }
  }
});

const Student = Person.extend ({
  capabilties: {
    a: { a: 3, c: 5}
    c: { a: 1 }
  }
});

let person = new Person ();
console.log (person.capabilities);       // {a: { a: 1, b: 3}, b: {a: 6} }

let student = new Student ();
console.log (student.capabilities);      // {a: { a: 3, b: 3, c: 5}, b: {a: 6}, c: { a: 1 }}

The merged properties also apply when initializing a created object.

Last updated