YUI Library Examples: YAHOO Global Object: Creating Class Hierarchies with YAHOO.lang.extend

YAHOO Global Object: Creating Class Hierarchies with YAHOO.lang.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 YAHOO.example.Chicken();

YAHOO.lang is packaged with the YAHOO Global Object

YAHOO.lang comes bundled with the YAHOO Global Object. To add the YAHOO Global Object, include the following in your markup:

1<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo/yahoo-min.js"></script> 
view plain | print | ?

If you are using any other YUI component on your page, you should already have YAHOO.lang available.

Creating a class hierarchy

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

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

instanceof many classes

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

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

1YAHOO.util.Event.on('demo_btn','click'function () { 
2 
3    // Create a test Chicken 
4    var chicken = new Ye.Chicken('Little'); 
5 
6    var results = YAHOO.util.Dom.get('demo'); 
7 
8    // Is the chicken an instanceof Object? 
9    results.innerHTML = chicken instanceof Object ? 
10                         "<p>chicken IS an instance of Object.</p>" : 
11                         "<p>chicken IS NOT an instance of Object.</p>"
12 
13    // Is the chicken an instanceof YAHOO.example.Bird? 
14    results.innerHTML += chicken instanceof Ye.Bird ? 
15                         "<p>chicken IS an instance of YAHOO.example.Bird.</p>" : 
16                         "<p>chicken IS NOT an instance of YAHOO.example.Bird.</p>"
17 
18    // Is the chicken an instanceof YAHOO.example.Chicken? 
19    results.innerHTML += chicken instanceof Ye.Chicken ? 
20                         "<p>chicken IS an instance of YAHOO.example.Chicken.</p>" : 
21                         "<p>chicken IS NOT an instance of YAHOO.example.Chicken.</p>"
22 
23    // instances inherit methods and members from their superclass and ancestors 
24    results.innerHTML += chicken.isFlighted() ? 
25                         "<p>chicken CAN fly.</p>" : 
26                         "<p>chicken CAN NOT fly.</p>"
27 
28    results.innerHTML += "<p>chicken's name is " + chicken.getName() + ".</p>"
29}); 
view plain | print | ?

Other architecture strategies

Take a look at YAHOO.lang.augmentProto and YAHOO.lang.augmentObject for different strategies of managing your code structure.

Copyright © 2008 Yahoo! Inc. All rights reserved.

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