5
<title>Example: Compose Classes of Objects with `augment`</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Compose Classes of Objects with `augment`</h1>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
23
<p>Creating a composition-based class structure using <code>augment</code></p>
28
YUI().use('node', function(Y) {
30
YUI().use('node', function(Y) {
31
// This method is in the 'oop' module. Since we require 'node'
32
// for this example, and 'node' requires 'oop', the 'oop' module
33
// will be loaded automatically.
35
var Foo = function() {
36
/* code specific to Foo */
37
this.publish('interestingMoment');
40
Foo.prototype.doSomething = function() {
44
// -- do something interesting, add results to eventData --
46
eventData.statusText = 'bar';
48
// notify the subscribers, passing the event data
49
this.fire('interestingMoment', eventData);
52
Y.augment(Foo, Y.EventTarget);
56
// add some event listeners
57
foo.on('interestingMoment', function (data) {
58
var p = Y.one('#demo_p1');
59
p.setContent('I was notified of an interesting moment: ' + data.statusText);
62
foo.on('interestingMoment', function (data) {
63
var p = Y.one('#demo_p2');
64
p.setContent('I was also notified of an interesting moment: ' + data.statusText);
67
Y.on('click', function() {
77
<input type="button" id="demo" name="demo" value="Send">
86
<h3 id="using-augment">Using <code>augment</code></h3>
88
<pre class="code prettyprint">YUI().use('node', function(Y) {
89
// This method is in the 'oop' module. Since we require 'node'
90
// for this example, and 'node' requires 'oop', the 'oop' module
91
// will be loaded automatically.</pre>
94
<h3 id="the-example-any-class-can-be-an-eventtarget">The example: Any class can be an <code>EventTarget</code></h3>
96
<p>This example creates a custom class, then augments it with
97
<code>EventTarget</code>. Using the packaged functionality of <code>EventTarget</code>, the code for
98
<code>Foo</code> is able to focus on the functionality unique to its
101
<pre class="code prettyprint">YUI().use('node', function(Y) {
102
// This method is in the 'oop' module. Since we require 'node'
103
// for this example, and 'node' requires 'oop', the 'oop' module
104
// will be loaded automatically.
106
var Foo = function() {
107
/* code specific to Foo */
108
this.publish('interestingMoment');
111
Foo.prototype.doSomething = function() {
115
// -- do something interesting, add results to eventData --
117
eventData.statusText = 'bar';
119
// notify the subscribers, passing the event data
120
this.fire('interestingMoment', eventData);
123
Y.augment(Foo, Y.EventTarget);
127
// add some event listeners
128
foo.on('interestingMoment', function (data) {
129
var p = Y.one('#demo_p1');
130
p.setContent('I was notified of an interesting moment: ' + data.statusText);
133
foo.on('interestingMoment', function (data) {
134
var p = Y.one('#demo_p2');
135
p.setContent('I was also notified of an interesting moment: ' + data.statusText);
138
Y.on('click', function() {
140
}, '#demo');
144
<h3 id="composition-not-inheritance">Composition, not inheritance</h3>
145
<p>If <code>Foo</code> were a part of a class hierarchy, it would be improper
146
to include <code>EventTarget</code> in the inheritance chain, because its custom event
147
functionality is not an intrinsic characteristic but rather an ancillary, generic
148
capability that many classes share.</p>
150
<p>Unlike <code>extend</code>ed classes, the relationship between a class and
151
the classes augmenting it is not an indication of type hierarchy. The intent
152
of <code>augment</code> is to aid in extracting nonessential behaviors or
153
behaviors shared by many classes, allowing for a composition-style class
156
<img src="../assets/yui/composition_diagram.png" alt="Diagram showing class hierarchy, highlighting has-a relationship"/>
158
<p>This may appear similar to multiple inheritance, but it's not.
159
<code>augment</code> simply adds the public methods and members from one class
160
prototype to another class prototype. Instances of the augmented class will
161
not pass <code>instanceof</code> tests for the class(es) which augmented
164
<pre class="code prettyprint">YUI().use('oop', function(Y) {
167
Foo.prototype.doSomething = function () { /* something */ };
173
if (b instanceof Bar) {} // true
174
if (b instanceof Foo) {} // FALSE
180
<div id="sidebar" class="yui3-u">
182
<div id="toc" class="sidebox">
184
<h2 class="no-toc">Table of Contents</h2>
190
<a href="#using-augment">Using <code>augment</code></a>
193
<a href="#the-example-any-class-can-be-an-eventtarget">The example: Any class can be an <code>EventTarget</code></a>
196
<a href="#composition-not-inheritance">Composition, not inheritance</a>
204
<div class="sidebox">
206
<h2 class="no-toc">Examples</h2>
210
<ul class="examples">
213
<li data-description="Setting up a YUI Instance">
214
<a href="yui-core.html">YUI Core</a>
219
<li data-description="Working with multiple YUI instances.">
220
<a href="yui-multi.html">Multiple Instances</a>
225
<li data-description="On-demand loading of YUI and non-YUI assets">
226
<a href="yui-loader-ext.html">YUI Loader - Dynamically Adding YUI and External Modules</a>
231
<li data-description="Create Class Hierarchies with `extend`">
232
<a href="yui-extend.html">Create Class Hierarchies with `extend`</a>
237
<li data-description="Creating a composition-based class structure using `augment`">
238
<a href="yui-augment.html">Compose Classes of Objects with `augment`</a>
243
<li data-description="Add behaviors to objects or static classes with `mix`">
244
<a href="yui-mix.html">Add Behaviors to Objects with `mix`</a>
249
<li data-description="Combine data sets and create shallow copies of objects with `merge`">
250
<a href="yui-merge.html">Combine Data Sets with `merge`</a>
255
<li data-description="Check data types with the `Lang Utilities`">
256
<a href="yui-isa.html">Check Data Types with `Lang`</a>
261
<li data-description="Get information about the current user agent with `UA`">
262
<a href="yui-ua.html">Browser Detection with `UA`</a>
267
<li data-description="Working with YUI 2 in 3">
268
<a href="yui-yui2.html">Working with YUI 2 in 3</a>
273
<li data-description="Natively use YUI Gallery Modules">
274
<a href="yui-gallery.html">Natively use YUI Gallery Modules</a>
288
<script src="../assets/vendor/prettify/prettify-min.js"></script>
289
<script>prettyPrint();</script>