YUI recommends YUI3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

This documentation is no longer maintained.

YUI Library Examples: YAHOO Global Object: Creating a Composition-Based Class Structure Using YAHOO.lang.augmentProto

YAHOO Global Object: Creating a Composition-Based Class Structure Using YAHOO.lang.augmentProto

You may be familiar with this method as YAHOO.augment. See the history of augmentProto for background and recommendations for syntax.

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

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

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.

The example: Any class can be an EventProvider

This example creates a custom class, then augments it with YAHOO.util.EventProvider (functionality included in the YUI Event Utility). Using the packaged functionality of EventProvider, 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 EventProvider in the inheritance chain, since the purpose of the two are fundamentally different.

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

" alt="Diagram showing class hierarchy, highlighting has-a relationship"/>

This may appear similar to multiple inheritance, but it's not. augmentProto 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.

A short history of augmentProto

augmentProto started its life as YAHOO.augment way back in version 0.12 of YUI.

In version 2.2.0, it was moved into the YAHOO.lang module. The alias YAHOO.augment was added for backward compatibility.

In version 2.3.0, augment became augmentProto. Another alias, YAHOO.lang.augment, was created for backward compatibility.

While these aliases should be around for a while, it's advisable to use YAHOO.lang.augmentProto going forward to ensure that your code is as future-compatible as possible.

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings