Basic OOP / Class-Based Programming Concepts

JavaScript is a classless, prototype-oriented language and one of its most powerful features is flexibility. That said, Class-based programming is arguably the most popular model of Object Oriented Programming (OOP). This style generally emphasizes strong-typing, encapsulation, and standard coding conventions.

JavaScript’s flexibility comes with the cost of being unpredictable. Without a unified structure, JavaScript code can be difficult to understand, maintain, and re-use. On the other hand, class-based code is more likely to be predictable, extensible, and scalable over time.

Fortunately, Ext JS’ class system provides you with the best of both worlds. You gain a flexible, extensible, and scalable implementation of class-based programming with JavaScript’s flexibility.

This guide is intended for any developer that wants to learn or review Ext JS’ concept of OOP and class-based programming. We will cover the following topics:

  • Classes and Instances

  • Inheritance (polymorphism)

  • Encapsulation

Classes and Instances

It is important to be able to clearly distinguish between classes and instances. In simple terms, a class is the blueprint of a concept, while an instance is the actualization of the blueprint. Let’s look at some examples:

  • “Building” is a class, while the Empire State Building is an instance of “Building”.

  • “Dog” is a class, while Lassie is an instance of “Dog”.

  • “Computer” is a class, while the computer you’re using is an instance of “Computer”.

image alt text

A class defines the base structure, properties, and behavior of its instances. For example, using the same classes described above:

  • All instances of “Building” have a given number of floors (structure), an address, and opening hours (properties). Also, assuming these are “smart buildings”, they can close and lock their main entrance as needed (behavior).

  • All instances of “Dog” have 4 legs and a tail (structure). They also have a name (property) and are able to bark (behavior).

  • All instances of “Computer” have a CPU and some form of memory (structure), a model name (property), and are able to be turned on and off (behavior).

Let’s define a class that will serve as our base for exploring concepts of class-based programming. We’ll start with the “Square” class, which represents a square along with a simple method for calculating its area.

You can define the Square class with the following syntax:

  1. // Define a new class named: 'Square'
  2. Ext.define('Square', {
  3. // The side property represents the length of the side
  4. // It has a default value of 0
  5. side: 0,
  6. // It also has a method to calculate its area
  7. getArea: function() {
  8. // We access the 'side' property to calculate area
  9. return this.side * this.side;
  10. }
  11. });
  12. // We use Ext.create to create an instance of our Square class
  13. var sq = Ext.create('Square');
  14. // The value of the 'side' property
  15. // This is not the best way to do this, which we'll discuss below
  16. sq.side = 4;
  17. // Display a message and show the result of this calculation
  18. Ext.Msg.alert('Message', 'The area is: ' + sq.getArea());

This is a bare-bones implementation of a class using Ext JS. While it does meet our goals of representing a square and providing a method to calculate its area, it is not ideal or good practice.

Constructors

Let’s improve this example by utilizing a constructor. A constructor is a special function that gets called when a Class is instantiated. First, let’s change the way we set the value of Square’s side. By utilizing the constructor, we can remove the ‘ugly’ line from the example above.

  1. Ext.define('Square', {
  2. side: 0,
  3. // This is a special function that gets called
  4. // when the object is instantiated
  5. constructor: function (side) {
  6. // It receives the side as a parameter
  7. // If defined, it is set as the square's side value
  8. if (side) {
  9. this.side = side;
  10. }
  11. },
  12. getArea: function () {
  13. return this.side * this.side;
  14. }
  15. });
  16. // Thanks to the constructor, we can pass 'side's' value
  17. // as an argument of Ext.create
  18. // This is a slightly more elegant approach.
  19. var sq = Ext.create('Square', 4);
  20. // The passed value is assigned to the square's side property
  21. // Display a message to make sure everything is working
  22. Ext.Msg.alert('Message', 'The area is: ' + sq.getArea());

If you want to pass two or more property values to the constructor, you can do it using an object literal as follows:

  1. Ext.define('Square', {
  2. side: 0,
  3. // We have added two more configs
  4. color: 'red',
  5. border: true,
  6. // Pass a config object, which contains 'side's' value
  7. constructor: function(config) {
  8. // Once again, this is not yet the best syntax
  9. // We'll get to that in the next example
  10. if (config.side) {
  11. this.side = config.side;
  12. }
  13. if (config.color) {
  14. this.color = config.color;
  15. }
  16. // border is a boolean so we can skip the if block
  17. this.border = config.border;
  18. },
  19. getArea: function() {
  20. return this.side * this.side;
  21. }
  22. });
  23. // We pass an object containing properties/values
  24. var sq = Ext.create('Square', {
  25. side: 4,
  26. border: false
  27. });
  28. // Now display a message that uses the other two properties
  29. // Note that we're accessing them directly (i.e.: sq.color)
  30. // This will change in the next section
  31. Ext.Msg.alert('Message',
  32. ['The area of the',sq.color,'square',
  33. (sq.border?'with a border':''),'is:',
  34. sq.getArea()].join(' ')
  35. );

Apply

We can clean up the constructor further using Ext.apply. Ext.apply copies all the properties of config to the specified object.

Note: The constructor will change again in the inheritance section.

  1. Ext.define('Square', {
  2. side: 0,
  3. color: 'red',
  4. border: true,
  5. constructor: function(config) {
  6. // Use Ext.apply to not set each property manually
  7. // We'll change this again in the "Inheritance" section
  8. Ext.apply(this,config);
  9. },
  10. getArea: function() {
  11. return this.side * this.side;
  12. }
  13. });
  14. var sq = Ext.create('Square', {
  15. side: 4,
  16. border: false
  17. });
  18. Ext.Msg.alert('Message',
  19. ['The area of the',sq.color,'square',
  20. (sq.border?'with a border':''),'is:',
  21. sq.getArea()].join(' ')
  22. );

Defining more classes

Let’s add Circle and Rectangle classes in order to show a few slight deviations from the Square example.

  1. Ext.define('Square', {
  2. side: 0,
  3. color: 'red',
  4. border: true,
  5. constructor: function(config) {
  6. Ext.apply(this, config);
  7. },
  8. getArea: function() {
  9. return this.side * this.side;
  10. }
  11. });
  12. Ext.define('Rectangle', {
  13. //Instead of side, a rectangle cares about base and height
  14. base: 0,
  15. height: 0,
  16. color: 'green',
  17. border: true,
  18. constructor: function(config) {
  19. Ext.apply(this, config);
  20. },
  21. getArea: function() {
  22. // The formula is different
  23. return this.base * this.height;
  24. }
  25. });
  26. Ext.define('Circle', {
  27. // A circle has no sides, but radius
  28. radius: 0,
  29. color: 'blue',
  30. border: true,
  31. constructor: function(config) {
  32. Ext.apply(this, config);
  33. },
  34. getArea: function() {
  35. // Just for this example, fix the precision of PI to 2
  36. return Math.PI.toFixed(2) * Math.pow(this.radius, 2);
  37. }
  38. });
  39. var square = Ext.create('Square', {
  40. side: 4,
  41. border: false
  42. }),
  43. rectangle = Ext.create('Rectangle', {
  44. base: 4,
  45. height: 3
  46. }),
  47. circle = Ext.create('Circle', {
  48. radius: 3
  49. });
  50. // This message will now show a line for each object
  51. Ext.Msg.alert('Message', [
  52. ['The area of the', square.color, 'square',
  53. (square.border ? 'with a border' : ''), 'is:',
  54. square.getArea()].join(' '),
  55. ['The area of the', rectangle.color, 'rectangle',
  56. (rectangle.border ? 'with a border' : ''), 'is:',
  57. rectangle.getArea()].join(' '),
  58. ['The area of the', circle.color, 'circle',
  59. (circle.border ? 'with a border' : ''), 'is:',
  60. circle.getArea()].join(' ')
  61. ].join('<br />'));

Inheritance

Before diving into the concept of inheritance, let’s review the following example. As you can see below, we’ve added an additional method to the Square class and changed the way the test message is generated:

  1. Ext.define('Square', {
  2. side: 0,
  3. color: 'red',
  4. border: true,
  5. constructor: function(config) {
  6. Ext.apply(this, config);
  7. },
  8. getArea: function() {
  9. return this.side * this.side;
  10. },
  11. // This function will return the name of this shape
  12. getShapeName: function () {
  13. return 'square';
  14. }
  15. });
  16. //This function generates a sentence to display in the test dialog
  17. function generateTestSentence(shape) {
  18. return ['The area of the', shape.color, shape.getShapeName(),
  19. (shape.border ? 'with a border' : ''),
  20. 'is:', shape.getArea()].join(' ');
  21. }
  22. var square = Ext.create('Square', {
  23. side: 4,
  24. border: false
  25. });
  26. Ext.Msg.alert('Message', generateTestSentence(square));

In the next example, we’ll apply the same changes to the Rectangle and Circle classes:

  1. Ext.define('Square', {
  2. side: 0,
  3. color: 'red',
  4. border: true,
  5. constructor: function(config) {
  6. Ext.apply(this, config);
  7. },
  8. getArea: function() {
  9. return this.side * this.side;
  10. },
  11. getShapeName: function () {
  12. return 'square';
  13. }
  14. });
  15. Ext.define('Rectangle', {
  16. base: 0,
  17. height: 0,
  18. color: 'green',
  19. border: true,
  20. constructor: function(config) {
  21. Ext.apply(this, config);
  22. },
  23. getArea: function() {
  24. return this.base * this.height;
  25. },
  26. getShapeName: function () {
  27. return 'rectangle';
  28. }
  29. });
  30. Ext.define('Circle', {
  31. radius: 0,
  32. color: 'blue',
  33. border: true,
  34. constructor: function(config) {
  35. Ext.apply(this, config);
  36. },
  37. getArea: function() {
  38. return Math.PI.toFixed(2) * Math.pow(this.radius, 2);
  39. },
  40. getShapeName: function () {
  41. return 'circle';
  42. }
  43. });
  44. // Generates a sentence that will be displayed in the test dialog
  45. function generateTestSentence(shape) {
  46. return ['The area of the', shape.color, shape.getShapeName(),
  47. (shape.border ? 'with a border' : ''), 'is:',
  48. shape.getArea()].join(' ');
  49. }
  50. var square = Ext.create('Square', {
  51. side: 4,
  52. border: false
  53. }),
  54. rectangle = Ext.create('Rectangle', {
  55. base: 4,
  56. height: 3
  57. }),
  58. circle = Ext.create('Circle', {
  59. radius: 3
  60. });
  61. Ext.Msg.alert('Message', [
  62. generateTestSentence(square),
  63. generateTestSentence(rectangle),
  64. generateTestSentence(circle)
  65. ].join('<br />'));

If you carefully review the above example, you may notice a lot of repetition. This can make your code difficult to maintain and prone to errors. The concept of inheritance helps us consolidate repetitive code and makes it easier to understand and maintain.

Parent and child classes

By applying the concept of inheritance, we can simplify and reduce the repetitive code by giving child classes properties of a parent class:

  1. // The shape class contains common code to each shape class
  2. // This allows the passing of properties on child classes
  3. Ext.define('Shape', {
  4. // Let's define common properties here and set default values
  5. color: 'gray',
  6. border: true,
  7. // Let's add a shapeName property and a method to return it
  8. // This replaces unique getShapeName methods on each class
  9. shapeName: 'shape',
  10. constructor: function (config) {
  11. Ext.apply(this, config);
  12. },
  13. getShapeName: function () {
  14. return this.shapeName;
  15. }
  16. });
  17. Ext.define('Square', {
  18. // Square extends from Shape so it gains properties
  19. // defined on itself and its parent class
  20. extend: 'Shape',
  21. // These properties will 'override' parent class properties
  22. side: 0,
  23. color: 'red',
  24. shapeName: 'square',
  25. getArea: function() {
  26. return this.side * this.side;
  27. }
  28. });
  29. //This function generates a sentence to display in the test dialog
  30. function generateTestSentence(shape) {
  31. return ['The area of the', shape.color, shape.getShapeName(),
  32. (shape.border ? 'with a border' : ''),
  33. 'is:', shape.getArea()].join(' ');
  34. }
  35. var square = Ext.create('Square', {
  36. side: 4
  37. });
  38. // Since Square extends from Shape, this example will work since
  39. // all other properties are still defined, but now by 'Shape'
  40. Ext.Msg.alert('Message',
  41. [ generateTestSentence(square) ].join('<br />'));

We can even move the generateTestSentence() method to the Shape class:

  1. Ext.define('Shape', {
  2. color: 'gray',
  3. border: true,
  4. shapeName: 'shape',
  5. constructor: function (config) {
  6. Ext.apply(this, config);
  7. },
  8. getShapeName: function () {
  9. return this.shapeName;
  10. },
  11. // This function will generate the test sentence for this shape,
  12. // so no need to pass it as an argument
  13. getTestSentence: function () {
  14. return ['The area of the', this.color, this.getShapeName(),
  15. (this.border ? 'with a border' : ''),
  16. 'is:', this.getArea()].join(' ');
  17. }
  18. });
  19. Ext.define('Square', {
  20. extend: 'Shape',
  21. side: 0,
  22. color: 'red',
  23. shapeName: 'square',
  24. getArea: function() {
  25. return this.side * this.side;
  26. }
  27. });
  28. var square = Ext.create('Square', {
  29. side: 4
  30. });
  31. // The generateTestSentence function doesn't exist anymore
  32. // so use the one that comes with the shape
  33. Ext.Msg.alert('Message',
  34. [ square.getTestSentence() ].join('<br />'));

As you can see, the properties on the child class will override properties on the parent class if they’re both set. For instance, the Shape’s shapeName is “shape”. However, since shapeName is set on the Square class as well, it overrides the parent class’s value. If the child class doesn’t have a property set, it will inherit said property from the parent.

Encapsulation

In the previous examples, you may notice we’re accessing instance properties by calling them directly. For instance, getting square’s color by accessing “square.color”. You can set the value directly as well:

  1. Ext.define('Shape', {
  2. color: 'gray',
  3. border: true,
  4. shapeName: 'shape',
  5. constructor: function (config) {
  6. Ext.apply(this, config);
  7. },
  8. getShapeName: function () {
  9. return this.shapeName;
  10. },
  11. getTestSentence: function () {
  12. return ['The area of the', this.color, this.getShapeName(),
  13. (this.border ? 'with a border' : ''),
  14. 'is:', this.getArea()].join(' ');
  15. }
  16. });
  17. Ext.define('Square', {
  18. extend: 'Shape',
  19. side: 0,
  20. color: 'red',
  21. shapeName: 'square',
  22. getArea: function() {
  23. return this.side * this.side;
  24. }
  25. });
  26. var square = Ext.create('Square', {
  27. side: 4
  28. });
  29. // Set the value of 'side' to 5 instead of the initial 4
  30. // While not bad, this is something that should be avoided
  31. square.side = 5;
  32. // Set the value of 'side' to a string instead of a number
  33. // String is not a valid value. This is an example of why
  34. // direct access to the properties should be avoided.
  35. // Open access is prone to error.
  36. square.side = 'five';
  37. // The area will be reported as NaN
  38. Ext.Msg.alert('Message',
  39. [ square.getTestSentence() ].join('<br />'));

Config Block

To prevent direct read/write of an object’s properties, we’ll make use of Ext JS’ config block. This will automatically restrict access to the object’s properties so they can only be set and retrieved using accessor methods.

Accessor methods are automatically generated getters and setters for anything in a class’s config block. For instance, if you have shapeName in a config block, you get setShapeName() and getShapeName() by default.

Note: The config block should only include new configs unique to its class. You should not include configs already defined in a parent class’s config block.

  1. Ext.define('Shape', {
  2. // All properties inside the config block have
  3. // their accessor methods automatically generated
  4. config: {
  5. color: 'gray', // creates getColor|setColor
  6. border: true, // creates getBorder|setBorder
  7. shapeName: 'shape' // creates getShapeName|setShapeName
  8. },
  9. constructor: function (config) {
  10. Ext.apply(this, config);
  11. // Initialize the config block for this class
  12. // This auto-generates the accessor methods
  13. // More information on this in the next section
  14. this.initConfig(config);
  15. },
  16. // We have removed the getShapeName method
  17. // It's auto-generated since shapeName is in the config block
  18. // Now we can use the accessor methods instead
  19. // of accessing the properties directly
  20. getTestSentence: function () {
  21. return ['The area of the', this.getColor(),
  22. this.getShapeName(),
  23. (this.getBorder() ? 'with a border' : ''), 'is:',
  24. this.getArea()].join(' ');
  25. }
  26. });
  27. Ext.define('Square', {
  28. extend: 'Shape',
  29. // In a child class, the config block should only
  30. // contain new configs particular for this class
  31. config: {
  32. side: 0 // getSide and setSide are now available
  33. },
  34. // Parent class properties are defined outside the config block
  35. color: 'red',
  36. shapeName: 'square',
  37. getArea: function() {
  38. // We're using the accessor methods of the 'side' config
  39. return this.getSide() * this.getSide();
  40. }
  41. });
  42. var square = Ext.create('Square', {
  43. side: 4
  44. });
  45. // The following line won't modify the value of 'side' anymore
  46. square.side = 'five';
  47. // To modify it instead, we'll use the setSide method:
  48. square.setSide(5);
  49. // The area will be reported as 25
  50. Ext.Msg.alert('Message',
  51. [ square.getTestSentence() ].join('<br />'));

Ext.Base class

In Ext JS, all classes are children of a common base class unless explicitly specified. This base class is Ext.Base.

Just like our Square class extends from Shape, Shape automatically extends from Ext.Base.
Based on this logic, the following code:

  1. Ext.define('Shape', {
  2. // Properties and methods here
  3. });

is actually equivalent to this:

  1. Ext.define('Shape', {
  2. extend: 'Ext.Base'
  3. // Properties and methods here
  4. });

This is why we can use this.initConfig(config); in the constructor of Shape. initConfig() is a method of Ext.Base and is inherited by anything extending from it. initConfig() initializes the config block for its class and auto-generates the accessor methods.

Real property encapsulation

The main goal of encapsulation is to protect objects from unwanted and/or invalid property modification. These modifications would inevitably result in errors.

For example, when using the config block to avoid direct property modification, nothing is currently preventing invalid values from being passed to the accessor methods. That is, nothing prevents us from calling square.setSide(‘five’), which would result in an error since side expects a numeral.

Let’s prevent this by using the apply method. Apply is a template method which allows you to test the proposed value before modification. This method copies all of the properties of config to the specified object.

Since ‘side’ is a property defined through the ‘config’ block, we can make use of this template method to act before the value is actually modified, such as checking if the new value for ‘side’ is indeed a number.

  1. Ext.define('Shape', {
  2. config: {
  3. color: 'gray',
  4. border: true,
  5. shapeName: 'shape'
  6. },
  7. constructor: function (config) {
  8. Ext.apply(this, config);
  9. this.initConfig(config);
  10. },
  11. getTestSentence: function () {
  12. return ['The area of the', this.getColor(),
  13. this.getShapeName(),
  14. (this.getBorder() ? 'with a border' : ''),
  15. 'is:', this.getArea()].join(' ');
  16. }
  17. });
  18. Ext.define('Square', {
  19. extend: 'Shape',
  20. config: {
  21. side: 0
  22. },
  23. color: 'red',
  24. shapeName: 'square',
  25. getArea: function() {
  26. return this.getSide() * this.getSide();
  27. },
  28. // 'side' is a property defined through the 'config' block,
  29. // We can use this method before the value is modified
  30. // For instance, checking that 'side' is a number
  31. applySide: function (newValue, oldValue) {
  32. return (Ext.isNumber(newValue)? newValue : oldValue);
  33. }
  34. });
  35. var square = Ext.create('Square', {
  36. side: 4
  37. });
  38. // The following line won't modify the value of 'side'
  39. square.setSide('five');
  40. // The area will be reported as 16
  41. Ext.Msg.alert('Message',
  42. [ square.getTestSentence() ].join('<br />'));

Conclusion

We hope that this guide clarifies the basic concepts of OOP and Class-based programming in Ext JS. Be sure to check out the Class System guide for additional information on how to leverage Ext JS’ class system when making your own applications. As always, if you have questions regarding guide content, be sure to ask on the community forums or by submitting a Support ticket through the Support Portal (Sencha Support customer access only).