YUI 3.x Home -

YUI Library Examples: The YUI Global Object: Compose Classes of Objects with augment

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

The YUI Global Object: Compose Classes of Objects with augment

In this example, a class has been created then augmented with EventTarget to provide custom event functionality.

Click the Send button to send the instance of the new class a request.

Using augment

Instantiate YUI

The example: Any class can be an EventTarget

This example creates a custom class, then augments it with EventTarget (functionality included in the YUI Event Utility). Using the packaged functionality of EventTarget, the code for Foo is able to focus on the functionality unique to its purpose.

Composition, not inheritance

If Foo were a part of a class hierarchy, it would be improper to include EventTarget in the inheritance chain, because its custom event functionality is not an intrinsic characteristic but rather an ancillary, generic capability that many classes share.

Unlike extended classes, the relationship between a class and the classes augmenting it is not an indication of type hierarchy. The intent of augment is to aid in extracting nonessential behaviors or behaviors shared by many classes, allowing for a composition-style class architecture.

Diagram showing class hierarchy, highlighting has-a relationship

This may appear similar to multiple inheritance, but it's not. augment simply adds the public methods and members from one class prototype to another class prototype. Instances of the augmented class will not pass instanceof tests for the class(es) which augmented it.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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