5
<title>Example: Combine Data Sets with `merge`</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: Combine Data Sets with `merge`</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>Combine data sets and create shallow copies of objects with <code>merge</code></p>
27
<style type="text/css">
31
background-color: #fff;
44
<pre>set1 = { foo : "foo" };</pre>
45
<pre>set2 = { foo : "BAR", bar : "bar" };</pre>
46
<pre>set3 = { foo : "FOO", baz : "BAZ" };</pre>
48
<input type="button" name="demo_btn1" id="demo_btn1" value="Merge"/>
49
<input type="button" name="demo_btn2" id="demo_btn2" value="Copy"/>
50
<h3 id="result">Result</h3>
51
<div id="demo_result" class="intro">click Merge or Copy</div>
52
<script type="text/javascript">
54
YUI().use('node', 'dump', function(Y) {
56
var set1 = { foo : "foo" };
57
var set2 = { foo : "BAR", bar : "bar" };
58
var set3 = { foo : "FOO", baz : "BAZ" };
59
var result = Y.one('#demo_result');
61
var doMerge = function () {
63
Y.log('set1 = ' + Y.dump(set1));
64
Y.log('set2 = ' + Y.dump(set2));
65
Y.log('set3 = ' + Y.dump(set3));
67
Y.log('Merging set1, set2, and set3');
68
var merged = Y.merge(set1, set2, set3);
69
Y.log('merged = ' + Y.dump(merged));
71
result.setContent(Y.dump(merged));
74
Y.on('click', doMerge, '#demo_btn1');
77
var doCopy = function () {
79
// Create set4 with an object property 'obj'
84
// Create a shallow copy of set4
85
var copy = Y.merge(set4);
87
// Add a property to the copy inside of the 'obj' property
88
copy.obj.addedToCopy = true;
90
Y.log('After modifying the copy: ');
92
// The result object is not the same as the original, but
93
var msg = ('"copy" should NOT be equal to the "original" (false expected): ' + (copy === set4));
95
// objects in the result object will reference the same object in
97
msg += '<br><br>copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): ' +
98
(copy.obj.addedToCopy === set4.obj.addedToCopy);
101
result.setContent(msg);
104
Y.on('click', doCopy, '#demo_btn2');
118
<h3 id="using-merge">Using <code>merge</code></h3>
120
<pre class="code prettyprint">YUI().use('node', 'dump', function(Y) {
121
// This method is in the core of the library, so we don't have to use() any
122
// additional modules to access it. However, this example requires 'node' and 'dump'.</pre>
125
<h3 id="merging-hash-tables">Merging hash tables</h3>
126
<p>When the "Merge" button is clicked, we merge three object literals in the form
127
of hash tables. Note the key values in later parameters override those in
128
previous parameters.</p>
130
<pre class="code prettyprint">var set1 = { foo : "foo" };
131
var set2 = { foo : "BAR", bar : "bar" };
132
var set3 = { foo : "FOO", baz : "BAZ" };
133
var result = Y.one('#demo_result');
135
var doMerge = function () {
137
Y.log('set1 = ' + Y.dump(set1));
138
Y.log('set2 = ' + Y.dump(set2));
139
Y.log('set3 = ' + Y.dump(set3));
141
Y.log('Merging set1, set2, and set3');
142
var merged = Y.merge(set1, set2, set3);
143
Y.log('merged = ' + Y.dump(merged));
145
result.setContent(Y.dump(merged));
148
Y.on('click', doMerge, '#demo_btn1');</pre>
151
<h3 id="creating-shallow-copies">Creating Shallow Copies</h3>
152
<p>When the "Copy" button is clicked, we create use merge on a single
153
object in order to create a shallow clone. The code illustrates the
154
fact that object properties of the result object are shared with
155
the input object.</p>
157
<pre class="code prettyprint">var doCopy = function () {
159
// Create set4 with an object property 'obj'
164
// Create a shallow copy of set4
165
var copy = Y.merge(set4);
167
// Add a property to the copy inside of the 'obj' property
168
copy.obj.addedToCopy = true;
170
Y.log('After modifying the copy: ');
172
// The result object is not the same as the original, but
173
var msg = ('"copy" should NOT be equal to the "original" (false expected): ' + (copy === set4));
175
// objects in the result object will reference the same object in
176
// the input object.
177
msg += '<br><br>copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): ' +
178
(copy.obj.addedToCopy === set4.obj.addedToCopy);
181
result.setContent(msg);
184
Y.on('click', doCopy, '#demo_btn2');</pre>
187
<p>See the <code>clone</code> method to create deep copies of objects.</p>
189
<h3 id="full-javascript-source">Full Javascript Source</h3>
191
<pre class="code prettyprint">YUI().use('node', 'dump', function(Y) {
193
var set1 = { foo : "foo" };
194
var set2 = { foo : "BAR", bar : "bar" };
195
var set3 = { foo : "FOO", baz : "BAZ" };
196
var result = Y.one('#demo_result');
198
var doMerge = function () {
200
Y.log('set1 = ' + Y.dump(set1));
201
Y.log('set2 = ' + Y.dump(set2));
202
Y.log('set3 = ' + Y.dump(set3));
204
Y.log('Merging set1, set2, and set3');
205
var merged = Y.merge(set1, set2, set3);
206
Y.log('merged = ' + Y.dump(merged));
208
result.setContent(Y.dump(merged));
211
Y.on('click', doMerge, '#demo_btn1');
214
var doCopy = function () {
216
// Create set4 with an object property 'obj'
221
// Create a shallow copy of set4
222
var copy = Y.merge(set4);
224
// Add a property to the copy inside of the 'obj' property
225
copy.obj.addedToCopy = true;
227
Y.log('After modifying the copy: ');
229
// The result object is not the same as the original, but
230
var msg = ('"copy" should NOT be equal to the "original" (false expected): ' + (copy === set4));
232
// objects in the result object will reference the same object in
233
// the input object.
234
msg += '<br><br>copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): ' +
235
(copy.obj.addedToCopy === set4.obj.addedToCopy);
238
result.setContent(msg);
241
Y.on('click', doCopy, '#demo_btn2');
250
<div id="sidebar" class="yui3-u">
252
<div id="toc" class="sidebox">
254
<h2 class="no-toc">Table of Contents</h2>
260
<a href="#result">Result</a>
263
<a href="#using-merge">Using <code>merge</code></a>
266
<a href="#merging-hash-tables">Merging hash tables</a>
269
<a href="#creating-shallow-copies">Creating Shallow Copies</a>
272
<a href="#full-javascript-source">Full Javascript Source</a>
280
<div class="sidebox">
282
<h2 class="no-toc">Examples</h2>
286
<ul class="examples">
289
<li data-description="Setting up a YUI Instance">
290
<a href="yui-core.html">YUI Core</a>
295
<li data-description="Working with multiple YUI instances.">
296
<a href="yui-multi.html">Multiple Instances</a>
301
<li data-description="On-demand loading of YUI and non-YUI assets">
302
<a href="yui-loader-ext.html">YUI Loader - Dynamically Adding YUI and External Modules</a>
307
<li data-description="Create Class Hierarchies with `extend`">
308
<a href="yui-extend.html">Create Class Hierarchies with `extend`</a>
313
<li data-description="Creating a composition-based class structure using `augment`">
314
<a href="yui-augment.html">Compose Classes of Objects with `augment`</a>
319
<li data-description="Add behaviors to objects or static classes with `mix`">
320
<a href="yui-mix.html">Add Behaviors to Objects with `mix`</a>
325
<li data-description="Combine data sets and create shallow copies of objects with `merge`">
326
<a href="yui-merge.html">Combine Data Sets with `merge`</a>
331
<li data-description="Check data types with the `Lang Utilities`">
332
<a href="yui-isa.html">Check Data Types with `Lang`</a>
337
<li data-description="Get information about the current user agent with `UA`">
338
<a href="yui-ua.html">Browser Detection with `UA`</a>
343
<li data-description="Working with YUI 2 in 3">
344
<a href="yui-yui2.html">Working with YUI 2 in 3</a>
349
<li data-description="Natively use YUI Gallery Modules">
350
<a href="yui-gallery.html">Natively use YUI Gallery Modules</a>
364
<script src="../assets/vendor/prettify/prettify-min.js"></script>
365
<script>prettyPrint();</script>