YUI 3.x Home -

YUI Library Examples: The YUI Global Object: Add Behaviors to Objects with mix

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

The YUI Global Object: Add Behaviors to Objects with mix

Static classes in JavaScript 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 & and that's what YUI's mix method helps you to do.

Click the button below to call methods mixed into a static class, then read the tutorial below for more information about using mix.

Using mix

Instantiate YUI

Adding functionality to individual objects

Static classes, such as 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, mix is used to add a set of behaviors to a static class.

We'll create a namespace 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 mix while keeping its source focused and unique.

Much like augment

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

See augment and extend for other techniques to help manage your code structure.

Copyright © 2008 Yahoo! Inc. All rights reserved.

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