YUI 3.x Home -

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

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

  1. <!-- include yui -->
  2. <script type="text/javascript" src="../../build/yui/yui.js"></script>
  3.  
  4. YUI().use("node", function(Y) {
  5. // This method is in the core of the library, so we don't have to use() any
  6. // additional modules to access it. However, this example requires 'node'.
<!-- include yui -->
<script type="text/javascript" src="../../build/yui/yui.js"></script>
 
YUI().use("node", function(Y) {
    // This method is in the core of the library, so we don't have to use() any
    // additional modules to access it.  However, this example requires 'node'.

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.

  1. var Logging = function () {
  2. var logger = null;
  3.  
  4. return {
  5. initLogger : function (logNode) {
  6. if (!logger) {
  7. logger = Y.one(logNode);
  8. }
  9. },
  10.  
  11. log : function (message) {
  12. if (logger) {
  13. logger.set('innerHTML', logger.get('innerHTML') + '<p>' + message + '</p>');
  14. }
  15. }
  16. }
  17. }();
  18.  
  19. var PageController = function () {
  20. var app_const = 12345;
  21.  
  22. return {
  23.  
  24. getConst : function () {
  25. return app_const;
  26. },
  27.  
  28. logConst : function () {
  29. this.initLogger('#demo_logger');
  30. this.log('PageController class constant = ' + this.getConst() +
  31. ', logged courtesy of object augmentation via Y.mix.');
  32. }
  33. };
  34. }();
  35.  
  36. Y.mix(PageController, Logging);
  37.  
  38. Y.on('click', PageController.logConst, '#demo_btn', PageController);
var Logging = function () {
    var logger = null;
 
    return {
        initLogger : function (logNode) {
            if (!logger) {
                logger = Y.one(logNode);
            }
        },
 
        log : function (message) {
            if (logger) {
                logger.set('innerHTML', logger.get('innerHTML') + '<p>' + message + '</p>');
            }
        }
    }
}();
 
var PageController = function () {
    var app_const = 12345;
 
    return {
 
        getConst : function () { 
            return app_const;
        },
 
        logConst : function () {
            this.initLogger('#demo_logger');
            this.log('PageController class constant = ' + this.getConst() +
                      ', logged courtesy of object augmentation via Y.mix.');
        }
    };
}();
 
Y.mix(PageController, Logging);
 
Y.on('click', PageController.logConst, '#demo_btn', PageController);

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 © 2009 Yahoo! Inc. All rights reserved.

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