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.

Copyright © 2007 Yahoo! Inc. All rights reserved.

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