YUI 3.x Home -

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

Note: This is YUI 3.x. Looking for YUI 2.x?

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

Creating a class hierarchy

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

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.

Other architecture strategies

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

Copyright © 2009 Yahoo! Inc. All rights reserved.

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