YUI Library Home

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:

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.

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.

Other architecture strategies

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

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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