YUI 3.x Home -

YUI Library Examples: The YUI Global Object: Create Class Hierarchies with extend

The YUI Global Object: Create Class Hierarchies with extend

JavaScript supports the notion of class hierarchies. As in other object-oriented languages, you can create generic classes, then create specialized subclasses that add or override functionality of the superclass.

Click the "Show Inheritance" button to do some evaluation of the created object.

var chicken = new Chicken();

Using extend

Instantiate YUI

  1. <!-- include yui -->
  2. <script type="text/javascript" src="../../build/yui/yui.js"></script>
  3.  
  4. YUI().use("node", function(Y) {
  5. // This method is in the 'oop' module. Since we require 'node'
  6. // for this example, and 'node' requires 'oop', the 'oop' module
  7. // will be loaded automatically.
<!-- include yui -->
<script type="text/javascript" src="../../build/yui/yui.js"></script>
 
YUI().use("node", function(Y) {
    // This method is in the 'oop' module.  Since we require 'node'
    // for this example, and 'node' requires 'oop', the 'oop' module
    // will be loaded automatically.

Creating a class hierarchy

In this example, we create a class Bird then create a subclass Chicken.

  1. Bird = function (name) {
  2. this.name = name;
  3. };
  4.  
  5. Bird.prototype.flighted = true; // Default for all Birds
  6. Bird.prototype.isFlighted = function () { return this.flighted };
  7. Bird.prototype.getName = function () { return this.name };
  8.  
  9. Chicken = function (name) {
  10. // Chain the constructors
  11. Chicken.superclass.constructor.call(this, name);
  12. };
  13. // Chickens are birds
  14. Y.extend(Chicken, Bird);
  15.  
  16. // Define the Chicken prototype methods/members
  17. Chicken.prototype.flighted = false; // Override default for all Chickens
Bird = function (name) {
    this.name = name;
};
 
Bird.prototype.flighted   = true;  // Default for all Birds
Bird.prototype.isFlighted = function () { return this.flighted };
Bird.prototype.getName    = function () { return this.name };
 
Chicken = function (name) {
    // Chain the constructors
    Chicken.superclass.constructor.call(this, name);
};
// Chickens are birds
Y.extend(Chicken, Bird);
 
// Define the Chicken prototype methods/members
Chicken.prototype.flighted = false; // Override default for all Chickens

instanceof many classes

Unlike classes composed with augmentation, subclasses created with extend are also considered instances of their superclass and all classes higher up the inheritance tree.

We'll create an instance of Chicken and run some instanceof and method tests against it.

  1. showInheritance = function () {
  2. var chicken = new Chicken('Little'),
  3. results = Y.one('#demo');
  4.  
  5. results.set('innerHTML', results.get('innerHTML') +
  6. ((chicken instanceof Object) ?
  7. "<p>chicken IS an instance of Object.</p>" :
  8. "<p>chicken IS NOT an instance of Object.</p>"));
  9.  
  10. results.set('innerHTML', results.get('innerHTML') +
  11. ((chicken instanceof Bird) ?
  12. "<p>chicken IS an instance of Bird.</p>" :
  13. "<p>chicken IS NOT an instance of Bird.</p>"));
  14.  
  15. results.set('innerHTML', results.get('innerHTML') +
  16. ((chicken instanceof Chicken) ?
  17. "<p>chicken IS an instance of Chicken.</p>" :
  18. "<p>chicken IS NOT an instance of Chicken.</p>"));
  19.  
  20. // Chicken instances inherit Bird methods and members
  21. results.set('innerHTML', results.get('innerHTML') +
  22. ((chicken.isFlighted()) ?
  23. "<p>chicken CAN fly.</p>" :
  24. "<p>chicken CAN NOT fly.</p>"));
  25.  
  26. results.set('innerHTML', results.get('innerHTML') +
  27. "<p>chicken's name is " + chicken.getName() + ".</p>");
  28. }
  29.  
  30. Y.on('click', showInheritance, '#demo_btn');
showInheritance = function () {
    var chicken = new Chicken('Little'),
        results = Y.one('#demo');
 
    results.set('innerHTML', results.get('innerHTML') + 
        ((chicken instanceof Object) ?
            "<p>chicken IS an instance of Object.</p>" :
            "<p>chicken IS NOT an instance of Object.</p>"));
 
    results.set('innerHTML', results.get('innerHTML') + 
        ((chicken instanceof Bird) ?
            "<p>chicken IS an instance of Bird.</p>" :
            "<p>chicken IS NOT an instance of Bird.</p>"));
 
    results.set('innerHTML', results.get('innerHTML') + 
        ((chicken instanceof Chicken) ?
            "<p>chicken IS an instance of Chicken.</p>" :
            "<p>chicken IS NOT an instance of Chicken.</p>"));
 
    // Chicken instances inherit Bird methods and members
    results.set('innerHTML', results.get('innerHTML') + 
        ((chicken.isFlighted()) ?
            "<p>chicken CAN fly.</p>" :
            "<p>chicken CAN NOT fly.</p>"));
 
    results.set('innerHTML', results.get('innerHTML') + 
        "<p>chicken's name is " + chicken.getName() + ".</p>");
}
 
Y.on('click', showInheritance, '#demo_btn');

Other architecture strategies

Take a look at augment and mix for different strategies of managing your code structure.

Full Source

  1. YUI().use("node", function(Y) {
  2. // This method is in the 'oop' module. Since we require 'node'
  3. // for this example, and 'node' requires 'oop', the 'oop' module
  4. // will be loaded automatically.
  5.  
  6. var Bird = function (name) {
  7. this.name = name;
  8. };
  9.  
  10. Bird.prototype.flighted = true; // Default for all Birds
  11. Bird.prototype.isFlighted = function () { return this.flighted };
  12. Bird.prototype.getName = function () { return this.name };
  13.  
  14. Chicken = function (name) {
  15. // Chain the constructors
  16. Chicken.superclass.constructor.call(this, name);
  17. };
  18.  
  19. // Chickens are birds
  20. Y.extend(Chicken, Bird);
  21.  
  22. // Define the Chicken prototype methods/members
  23. Chicken.prototype.flighted = false; // Override default for all Chickens
  24.  
  25. showInheritance = function () {
  26. var chicken = new Chicken('Little'),
  27. results = Y.one('#demo');
  28.  
  29. results.set('innerHTML', results.get('innerHTML') +
  30. ((chicken instanceof Object) ?
  31. "<p>chicken IS an instance of Object.</p>" :
  32. "<p>chicken IS NOT an instance of Object.</p>"));
  33.  
  34. results.set('innerHTML', results.get('innerHTML') +
  35. ((chicken instanceof Bird) ?
  36. "<p>chicken IS an instance of Y.example.Bird.</p>" :
  37. "<p>chicken IS NOT an instance of Y.example.Bird.</p>"));
  38.  
  39. results.set('innerHTML', results.get('innerHTML') +
  40. ((chicken instanceof Chicken) ?
  41. "<p>chicken IS an instance of Y.example.Chicken.</p>" :
  42. "<p>chicken IS NOT an instance of Y.example.Chicken.</p>"));
  43.  
  44. // Chicken instances inherit Bird methods and members
  45. results.set('innerHTML', results.get('innerHTML') +
  46. ((chicken.isFlighted()) ?
  47. "<p>chicken CAN fly.</p>" :
  48. "<p>chicken CAN NOT fly.</p>"));
  49.  
  50. results.set('innerHTML', results.get('innerHTML') +
  51. "<p>chicken's name is " + chicken.getName() + ".</p>");
  52. }
  53.  
  54. Y.on('click', showInheritance, '#demo_btn');
  55. });
YUI().use("node", function(Y) {
    // This method is in the 'oop' module.  Since we require 'node'
    // for this example, and 'node' requires 'oop', the 'oop' module
    // will be loaded automatically.
 
    var Bird = function (name) {
        this.name = name;
    };
 
    Bird.prototype.flighted   = true;  // Default for all Birds
    Bird.prototype.isFlighted = function () { return this.flighted };
    Bird.prototype.getName    = function () { return this.name };
 
    Chicken = function (name) {
        // Chain the constructors
        Chicken.superclass.constructor.call(this, name);
    };
 
    // Chickens are birds
    Y.extend(Chicken, Bird);
 
    // Define the Chicken prototype methods/members
    Chicken.prototype.flighted = false; // Override default for all Chickens
 
    showInheritance = function () {
        var chicken = new Chicken('Little'),
            results = Y.one('#demo');
 
        results.set('innerHTML', results.get('innerHTML') +
            ((chicken instanceof Object) ?
                "<p>chicken IS an instance of Object.</p>" :
                "<p>chicken IS NOT an instance of Object.</p>"));
 
        results.set('innerHTML', results.get('innerHTML') + 
            ((chicken instanceof Bird) ?
                "<p>chicken IS an instance of Y.example.Bird.</p>" :
                "<p>chicken IS NOT an instance of Y.example.Bird.</p>"));
 
        results.set('innerHTML', results.get('innerHTML') + 
            ((chicken instanceof Chicken) ?
                "<p>chicken IS an instance of Y.example.Chicken.</p>" :
                "<p>chicken IS NOT an instance of Y.example.Chicken.</p>"));
 
        // Chicken instances inherit Bird methods and members
        results.set('innerHTML', results.get('innerHTML') + 
            ((chicken.isFlighted()) ?
                "<p>chicken CAN fly.</p>" :
                "<p>chicken CAN NOT fly.</p>"));
 
        results.set('innerHTML', results.get('innerHTML') + 
            "<p>chicken's name is " + chicken.getName() + ".</p>");
    }
 
    Y.on('click', showInheritance, '#demo_btn');
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings