5
<title>Example: Create Class Hierarchies with `extend`</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Create Class Hierarchies with `extend`</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><div class="intro">
23
<p>Create Class Hierarchies with <code>extend</code></p>
28
YUI().use('node', function(Y) {
34
Bird.prototype.flighted = true; // Default for all Birds
35
Bird.prototype.isFlighted = function () { return this.flighted };
36
Bird.prototype.getName = function () { return this.name };
38
function Chicken(name) {
39
// Chain the constructors
40
Chicken.superclass.constructor.call(this, name);
43
Y.extend(Chicken, Bird);
45
// Define the Chicken prototype methods/members
46
Chicken.prototype.flighted = false; // Override default for all Chickens
49
function showInheritance() {
50
var chicken = new Chicken('Little'),
51
results = Y.one('#demo');
53
results.setContent('Running: ' + (new Date()));
55
results.append(((chicken instanceof Object) ?
56
"<p>chicken IS an instance of Object.</p>" :
57
"<p>chicken IS NOT an instance of Object.</p>"));
59
results.append(((chicken instanceof Bird) ?
60
"<p>chicken IS an instance of Bird.</p>" :
61
"<p>chicken IS NOT an instance of Bird.</p>"));
63
results.append(((chicken instanceof Chicken) ?
64
"<p>chicken IS an instance of Chicken.</p>" :
65
"<p>chicken IS NOT an instance of Chicken.</p>"));
67
// Chicken instances inherit Bird methods and members
68
results.append(((chicken.isFlighted()) ?
69
"<p>chicken CAN fly.</p>" :
70
"<p>chicken CAN NOT fly.</p>"));
72
results.append("<p>chicken's name is " + chicken.getName() + ".</p>");
75
Y.on('click', showInheritance, '#demo_btn');
81
<button id="demo_btn">Click me</button>
86
<h3>Instantiate YUI</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>Creating a class hierarchy</h3>
95
<p>In this example, we create a class <code>Bird</code> then create a subclass <code>Chicken</code>.</p>
97
<pre class="code prettyprint">function Bird(name) {
101
Bird.prototype.flighted = true; // Default for all Birds
102
Bird.prototype.isFlighted = function () { return this.flighted };
103
Bird.prototype.getName = function () { return this.name };
105
function Chicken(name) {
106
// Chain the constructors
107
Chicken.superclass.constructor.call(this, name);
109
// Chickens are birds
110
Y.extend(Chicken, Bird);
112
// Define the Chicken prototype methods/members
113
Chicken.prototype.flighted = false; // Override default for all Chickens</pre>
116
<h3><code>instanceof</code> many classes</h3>
117
<p>Unlike classes composed with augmentation, subclasses created with <code>extend</code> are also
118
considered instances of their superclass and all classes higher up the
119
inheritance tree.</p>
121
<p>We'll create an instance of <code>Chicken</code> and run some <code>instanceof</code> and method tests against it.</p>
123
<pre class="code prettyprint">function showInheritance() {
124
var chicken = new Chicken('Little'),
125
results = Y.one('#demo');
127
results.setContent('Running: ' + (new Date()));
129
results.append(((chicken instanceof Object) ?
130
"<p>chicken IS an instance of Object.</p>" :
131
"<p>chicken IS NOT an instance of Object.</p>"));
133
results.append(((chicken instanceof Bird) ?
134
"<p>chicken IS an instance of Bird.</p>" :
135
"<p>chicken IS NOT an instance of Bird.</p>"));
137
results.append(((chicken instanceof Chicken) ?
138
"<p>chicken IS an instance of Chicken.</p>" :
139
"<p>chicken IS NOT an instance of Chicken.</p>"));
141
// Chicken instances inherit Bird methods and members
142
results.append(((chicken.isFlighted()) ?
143
"<p>chicken CAN fly.</p>" :
144
"<p>chicken CAN NOT fly.</p>"));
146
results.append("<p>chicken's name is " + chicken.getName() + ".</p>");
149
Y.on('click', showInheritance, '#demo_btn');</pre>
152
<h3>Other architecture strategies</h3>
153
<p>Take a look at <code>augment</code> and <code>mix</code> for different strategies of managing your code structure.
157
<pre class="code prettyprint">YUI().use('node', function(Y) {
159
function Bird(name) {
163
Bird.prototype.flighted = true; // Default for all Birds
164
Bird.prototype.isFlighted = function () { return this.flighted };
165
Bird.prototype.getName = function () { return this.name };
167
function Chicken(name) {
168
// Chain the constructors
169
Chicken.superclass.constructor.call(this, name);
171
// Chickens are birds
172
Y.extend(Chicken, Bird);
174
// Define the Chicken prototype methods/members
175
Chicken.prototype.flighted = false; // Override default for all Chickens
178
function showInheritance() {
179
var chicken = new Chicken('Little'),
180
results = Y.one('#demo');
182
results.setContent('Running: ' + (new Date()));
184
results.append(((chicken instanceof Object) ?
185
"<p>chicken IS an instance of Object.</p>" :
186
"<p>chicken IS NOT an instance of Object.</p>"));
188
results.append(((chicken instanceof Bird) ?
189
"<p>chicken IS an instance of Bird.</p>" :
190
"<p>chicken IS NOT an instance of Bird.</p>"));
192
results.append(((chicken instanceof Chicken) ?
193
"<p>chicken IS an instance of Chicken.</p>" :
194
"<p>chicken IS NOT an instance of Chicken.</p>"));
196
// Chicken instances inherit Bird methods and members
197
results.append(((chicken.isFlighted()) ?
198
"<p>chicken CAN fly.</p>" :
199
"<p>chicken CAN NOT fly.</p>"));
201
results.append("<p>chicken's name is " + chicken.getName() + ".</p>");
204
Y.on('click', showInheritance, '#demo_btn');
213
<div class="yui3-u-1-4">
214
<div class="sidebar">
218
<div class="sidebox">
220
<h2 class="no-toc">Examples</h2>
224
<ul class="examples">
227
<li data-description="Setting up a YUI Instance">
228
<a href="yui-core.html">YUI Core</a>
233
<li data-description="Working with multiple YUI instances.">
234
<a href="yui-multi.html">Multiple Instances</a>
239
<li data-description="On-demand loading of YUI and non-YUI assets">
240
<a href="yui-loader-ext.html">YUI Loader - Dynamically Adding YUI and External Modules</a>
245
<li data-description="Create Class Hierarchies with `extend`">
246
<a href="yui-extend.html">Create Class Hierarchies with `extend`</a>
251
<li data-description="Creating a composition-based class structure using `augment`">
252
<a href="yui-augment.html">Compose Classes of Objects with `augment`</a>
257
<li data-description="Add behaviors to objects or static classes with `mix`">
258
<a href="yui-mix.html">Add Behaviors to Objects with `mix`</a>
263
<li data-description="Combine data sets and create shallow copies of objects with `merge`">
264
<a href="yui-merge.html">Combine Data Sets with `merge`</a>
269
<li data-description="Check data types with the `Lang Utilities`">
270
<a href="yui-isa.html">Check Data Types with `Lang`</a>
275
<li data-description="Get information about the current user agent with `UA`">
276
<a href="yui-ua.html">Browser Detection with `UA`</a>
281
<li data-description="Working with YUI 2 in 3">
282
<a href="yui-yui2.html">Working with YUI 2 in 3</a>
287
<li data-description="Natively use YUI Gallery Modules">
288
<a href="yui-gallery.html">Natively use YUI Gallery Modules</a>
293
<li data-description="Programatically use Loader">
294
<a href="loader-resolve.html">Programatically use Loader</a>
299
<li data-description="Executing functions in parallel">
300
<a href="parallel.html">Using Y.Parallel</a>
315
<script src="../assets/vendor/prettify/prettify-min.js"></script>
316
<script>prettyPrint();</script>