YUI Library Home

YUI Library Examples: YAHOO Global Object: Add Behavior to Objects or Static Classes with YAHOO.lang.augmentObject

YAHOO Global Object: Add Behavior to Objects or Static Classes with YAHOO.lang.augmentObject

Click the button below to call methods on an augmented static class.

YAHOO.lang is packaged with the YAHOO Global Object

YAHOO.lang comes bundled with the YAHOO Global Object.

If you are using any other YUI component on your page, you should already have YAHOO.lang available.

Adding functionality to individual objects

Static classes, such as YAHOO.util.Dom, are implemented as object literals with keys corresponding to public class methods. As such, static classes aren't candidates for instantiation or prototype extention. To add functionality to static classes, you need to work with the class's object literal.

In this example, augmentObject is used to add a set of behaviors to a static class.

We'll create a namespace YAHOO.example.addons to hold common packages of static methods and members. In this namespace, we'll create a set of logging functions.

Now a targeted class that would benefit from these methods can add them using augmentObject while keeping its source focused and unique.

Much like YAHOO.lang.augmentProto

augmentObject works in similar fashion to augmentProto. In fact, augmentProto uses augmentObject under the hood. However, rather than adding functionality to class definitions (i.e. function prototypes), augmentObject can work with any object, including object literals and class instances.

See augmentProto and extend for other techniques to help manage 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