|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: The YUI Global Object: Combine Data Sets with <code>merge</code></title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
18 |
<script type="text/javascript" src="../../build/yui/yui-debug.js"></script> |
|
|
19 |
<style type="text/css"> |
|
|
20 |
#demo pre { |
|
|
21 |
font-size: .9em; |
|
|
22 |
padding: 1em; |
|
|
23 |
background-color: #fff; |
|
|
24 |
margin: 1ex; |
|
|
25 |
} |
|
|
26 |
#demo #demo_result { background-color: #ffe; } |
|
|
27 |
|
|
|
28 |
#demo h3 { |
|
|
29 |
margin: 1em 0 0 1ex; |
|
|
30 |
} |
|
|
31 |
</style> |
|
|
32 |
|
|
|
33 |
</head> |
|
|
34 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
35 |
<div id="custom-doc" class="yui-t2"> |
|
|
36 |
<div id="hd"> |
|
|
37 |
<div id="ygunav"> |
|
|
38 |
<p> |
|
|
39 |
<em> |
|
|
40 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
41 |
</em> |
|
|
42 |
</p> |
|
|
43 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
44 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
45 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
46 |
<div id="sitesearch"> |
|
|
47 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
48 |
<input type="text" id="searchinput" name="p"> |
|
|
49 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
50 |
</div> |
|
|
51 |
</form> |
|
|
52 |
</div> |
|
|
53 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
54 |
<div id="pagetitle"><h1>YUI Library Examples: The YUI Global Object: Combine Data Sets with <code>merge</code></h1></div> |
|
|
55 |
</div> |
|
|
56 |
<div id="bd"> |
|
|
57 |
|
|
|
58 |
|
|
|
59 |
<div id="yui-main"> |
|
|
60 |
<div class="yui-b"> |
|
|
61 |
<div class="yui-ge"> |
|
|
62 |
<div class="yui-u first example" id="main"> |
|
|
63 |
|
|
|
64 |
<h2>The YUI Global Object: Combine Data Sets with <code>merge</code></h2> |
|
|
65 |
|
|
|
66 |
<div id="example" class="promo"> |
|
|
67 |
<div class="example-intro"> |
|
|
68 |
<p><code>merge</code> allows you to combine any number of input objects and output a single merged set.<p> |
|
|
69 |
<p> |
|
|
70 |
If the input is a single object, the return value will be a shallow copy of the input, meaning that the return value |
|
|
71 |
is a different object, but any non-primitive value in the input object is referenced in the returned |
|
|
72 |
object. |
|
|
73 |
</p> |
|
|
74 |
|
|
|
75 |
<p>Click the "Merge" button to generate the merged set, the "Copy" button to create a shallow copy of an object.</p> |
|
|
76 |
</div> |
|
|
77 |
|
|
|
78 |
<div class="module example-container "> |
|
|
79 |
<div class="hd exampleHd"> |
|
|
80 |
<p class="newWindowButton yui-skin-sam"> |
|
|
81 |
<a href="yui-merge_clean.html" target="_blank">View example in new window.</a> |
|
|
82 |
</p> |
|
|
83 |
</div> <div id="example-canvas" class="bd"> |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
87 |
|
|
|
88 |
<div id="demo"> |
|
|
89 |
<pre>set1 = { foo : "foo" };</pre> |
|
|
90 |
<pre>set2 = { foo : "BAR", bar : "bar" };</pre> |
|
|
91 |
<pre>set3 = { foo : "FOO", baz : "BAZ" };</pre> |
|
|
92 |
|
|
|
93 |
<input type="button" name="demo_btn1" id="demo_btn1" value="Merge"/> |
|
|
94 |
<input type="button" name="demo_btn2" id="demo_btn2" value="Copy"/> |
|
|
95 |
<h3>result</h3> |
|
|
96 |
<pre id="demo_result">click Merge or Copy</pre> |
|
|
97 |
<script type="text/javascript"> |
|
|
98 |
|
|
|
99 |
YUI({base:"../../build/", timeout: 10000, filter:"debug", logInclude: {example:true}}).use("node", "dump", |
|
|
100 |
// This method is in the core of the library, so we don't have to use() any |
|
|
101 |
// additional modules to access it. However, this example requires 'node' |
|
|
102 |
// and 'dump'. |
|
|
103 |
|
|
|
104 |
function(Y) { |
|
|
105 |
|
|
|
106 |
var set1 = { foo : "foo" }; |
|
|
107 |
var set2 = { foo : "BAR", bar : "bar" }; |
|
|
108 |
var set3 = { foo : "FOO", baz : "BAZ" }; |
|
|
109 |
var result = Y.one('#demo_result'); |
|
|
110 |
|
|
|
111 |
var doMerge = function () { |
|
|
112 |
|
|
|
113 |
Y.log('set1 = ' + Y.dump(set1)); |
|
|
114 |
Y.log('set2 = ' + Y.dump(set2)); |
|
|
115 |
Y.log('set3 = ' + Y.dump(set3)); |
|
|
116 |
|
|
|
117 |
Y.log('Merging set1, set2, and set3'); |
|
|
118 |
var merged = Y.merge(set1, set2, set3); |
|
|
119 |
Y.log('merged = ' + Y.dump(merged)); |
|
|
120 |
|
|
|
121 |
result.set('innerHTML', '<p>' + stringifyObj(merged) + '</p>'); |
|
|
122 |
}; |
|
|
123 |
|
|
|
124 |
Y.on('click', doMerge, '#demo_btn1'); |
|
|
125 |
|
|
|
126 |
var doCopy = function () { |
|
|
127 |
|
|
|
128 |
// Create set4 with an object property 'obj' |
|
|
129 |
var set4 = { |
|
|
130 |
obj: {} |
|
|
131 |
}; |
|
|
132 |
|
|
|
133 |
// Create a shallow copy of set4 |
|
|
134 |
var copy = Y.merge(set4); |
|
|
135 |
|
|
|
136 |
// Add a property to the copy inside of the 'obj' property |
|
|
137 |
copy.obj.addedToCopy = true; |
|
|
138 |
|
|
|
139 |
Y.log('After modifying the copy: '); |
|
|
140 |
|
|
|
141 |
// The result object is not the same as the original, but |
|
|
142 |
var msg = ('"copy" should NOT be equal to the "original" (false expected): ' + (copy === set4)); |
|
|
143 |
|
|
|
144 |
// objects in the result object will reference the same object in |
|
|
145 |
// the input object. |
|
|
146 |
msg += '<br />copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): ' + |
|
|
147 |
(copy.obj.addedToCopy === set4.obj.addedToCopy); |
|
|
148 |
|
|
|
149 |
Y.log(msg); |
|
|
150 |
result.set('innerHTML', '<p>' + msg + '</p>'); |
|
|
151 |
}; |
|
|
152 |
|
|
|
153 |
Y.on('click', doCopy, '#demo_btn2'); |
|
|
154 |
|
|
|
155 |
var stringifyObj = function (obj) { |
|
|
156 |
var bits = ['{ ']; |
|
|
157 |
for (var k in obj) { |
|
|
158 |
bits = bits.concat([k, ' : "', obj[k], '", ']); |
|
|
159 |
} |
|
|
160 |
bits[bits.length - 1] = '" }'; |
|
|
161 |
|
|
|
162 |
return bits.join(''); |
|
|
163 |
}; |
|
|
164 |
|
|
|
165 |
}); |
|
|
166 |
</script> |
|
|
167 |
</div> |
|
|
168 |
|
|
|
169 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
</div> |
|
|
173 |
</div> |
|
|
174 |
</div> |
|
|
175 |
|
|
|
176 |
<h2 class="first">Using <code>merge</code></h2> |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
<h3 class="first">Instantiate YUI</h3> |
|
|
180 |
|
|
|
181 |
<div id="syntax1" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="sy0"><!--</span> include yui <span class="sy0">--></span></div></li><li class="li1"><div class="de1"><span class="sy0"><</span>script type<span class="sy0">=</span><span class="st0">"text/javascript"</span> src<span class="sy0">=</span><span class="st0">"../../build/yui/yui.js"</span><span class="sy0">></</span>script<span class="sy0">></span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">YUI<span class="br0">(</span><span class="br0">)</span>.<span class="kw2">use</span><span class="br0">(</span><span class="st0">"node"</span><span class="sy0">,</span> <span class="st0">"dump"</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">(</span>Y<span class="br0">)</span> <span class="br0">{</span></div></li><li class="li2"><div class="de2"> <span class="co1">// This method is in the core of the library, so we don't have to use() any</span></div></li><li class="li1"><div class="de1"> <span class="co1">// additional modules to access it. However, this example requires 'node'.</span></div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="sy0"><!--</span> include yui <span class="sy0">--></span> |
|
|
182 |
<span class="sy0"><</span>script type<span class="sy0">=</span><span class="st0">"text/javascript"</span> src<span class="sy0">=</span><span class="st0">"../../build/yui/yui.js"</span><span class="sy0">></</span>script<span class="sy0">></span> |
|
|
183 |
|
|
|
184 |
YUI<span class="br0">(</span><span class="br0">)</span>.<span class="kw2">use</span><span class="br0">(</span><span class="st0">"node"</span><span class="sy0">,</span> <span class="st0">"dump"</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">(</span>Y<span class="br0">)</span> <span class="br0">{</span> |
|
|
185 |
<span class="co1">// This method is in the core of the library, so we don't have to use() any</span> |
|
|
186 |
<span class="co1">// additional modules to access it. However, this example requires 'node'.</span></pre></div><textarea id="syntax1-plain"><!-- include yui --> |
|
|
187 |
<script type="text/javascript" src="../../build/yui/yui.js"></script> |
|
|
188 |
|
|
|
189 |
YUI().use("node", "dump", function(Y) { |
|
|
190 |
// This method is in the core of the library, so we don't have to use() any |
|
|
191 |
// additional modules to access it. However, this example requires 'node'.</textarea></div> |
|
|
192 |
<h3>Merging hash tables</h3> |
|
|
193 |
<p>When the "Merge" button is clicked, we merge three object literals in the form |
|
|
194 |
of hash tables. Note the key values in later parameters override those in |
|
|
195 |
previous parameters.</p> |
|
|
196 |
|
|
|
197 |
<div id="syntax2" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw2">var</span> set1 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"foo"</span> <span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> set2 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"BAR"</span><span class="sy0">,</span> bar <span class="sy0">:</span> <span class="st0">"bar"</span> <span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> set3 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"FOO"</span><span class="sy0">,</span> baz <span class="sy0">:</span> <span class="st0">"BAZ"</span> <span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="kw2">var</span> result <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">'#demo_result'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"><span class="kw2">var</span> doMerge <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set1 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set1<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set2 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set2<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set3 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set3<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'Merging set1, set2, and set3'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> <span class="kw2">var</span> merged <span class="sy0">=</span> Y.<span class="me1">merge</span><span class="br0">(</span>set1<span class="sy0">,</span> set2<span class="sy0">,</span> set3<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'merged = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>merged<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> result.<span class="me1">set</span><span class="br0">(</span><span class="st0">'innerHTML'</span><span class="sy0">,</span> <span class="st0">'<p>'</span> <span class="sy0">+</span> stringifyObj<span class="br0">(</span>merged<span class="br0">)</span> <span class="sy0">+</span> <span class="st0">'</p>'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1">Y.<span class="me1">on</span><span class="br0">(</span><span class="st0">'click'</span><span class="sy0">,</span> doMerge<span class="sy0">,</span> <span class="st0">'#demo_btn'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> set1 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"foo"</span> <span class="br0">}</span><span class="sy0">;</span> |
|
|
198 |
<span class="kw2">var</span> set2 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"BAR"</span><span class="sy0">,</span> bar <span class="sy0">:</span> <span class="st0">"bar"</span> <span class="br0">}</span><span class="sy0">;</span> |
|
|
199 |
<span class="kw2">var</span> set3 <span class="sy0">=</span> <span class="br0">{</span> foo <span class="sy0">:</span> <span class="st0">"FOO"</span><span class="sy0">,</span> baz <span class="sy0">:</span> <span class="st0">"BAZ"</span> <span class="br0">}</span><span class="sy0">;</span> |
|
|
200 |
<span class="kw2">var</span> result <span class="sy0">=</span> Y.<span class="me1">one</span><span class="br0">(</span><span class="st0">'#demo_result'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
201 |
|
|
|
202 |
<span class="kw2">var</span> doMerge <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
203 |
|
|
|
204 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set1 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set1<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
205 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set2 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set2<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
206 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'set3 = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>set3<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
207 |
|
|
|
208 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'Merging set1, set2, and set3'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
209 |
<span class="kw2">var</span> merged <span class="sy0">=</span> Y.<span class="me1">merge</span><span class="br0">(</span>set1<span class="sy0">,</span> set2<span class="sy0">,</span> set3<span class="br0">)</span><span class="sy0">;</span> |
|
|
210 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'merged = '</span> <span class="sy0">+</span> Y.<span class="me1">dump</span><span class="br0">(</span>merged<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
211 |
|
|
|
212 |
result.<span class="me1">set</span><span class="br0">(</span><span class="st0">'innerHTML'</span><span class="sy0">,</span> <span class="st0">'<p>'</span> <span class="sy0">+</span> stringifyObj<span class="br0">(</span>merged<span class="br0">)</span> <span class="sy0">+</span> <span class="st0">'</p>'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
213 |
<span class="br0">}</span><span class="sy0">;</span> |
|
|
214 |
|
|
|
215 |
Y.<span class="me1">on</span><span class="br0">(</span><span class="st0">'click'</span><span class="sy0">,</span> doMerge<span class="sy0">,</span> <span class="st0">'#demo_btn'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
216 |
</pre></div><textarea id="syntax2-plain">var set1 = { foo : "foo" }; |
|
|
217 |
var set2 = { foo : "BAR", bar : "bar" }; |
|
|
218 |
var set3 = { foo : "FOO", baz : "BAZ" }; |
|
|
219 |
var result = Y.one('#demo_result'); |
|
|
220 |
|
|
|
221 |
var doMerge = function () { |
|
|
222 |
|
|
|
223 |
Y.log('set1 = ' + Y.dump(set1)); |
|
|
224 |
Y.log('set2 = ' + Y.dump(set2)); |
|
|
225 |
Y.log('set3 = ' + Y.dump(set3)); |
|
|
226 |
|
|
|
227 |
Y.log('Merging set1, set2, and set3'); |
|
|
228 |
var merged = Y.merge(set1, set2, set3); |
|
|
229 |
Y.log('merged = ' + Y.dump(merged)); |
|
|
230 |
|
|
|
231 |
result.set('innerHTML', '<p>' + stringifyObj(merged) + '</p>'); |
|
|
232 |
}; |
|
|
233 |
|
|
|
234 |
Y.on('click', doMerge, '#demo_btn'); |
|
|
235 |
</textarea></div> |
|
|
236 |
<h3>Creating Shallow Copies</h3> |
|
|
237 |
<p>When the "Copy" button is clicked, we create use merge on a single |
|
|
238 |
object in order to create a shallow clone. The code illustrates the |
|
|
239 |
fact that object properties of the result object are shared with |
|
|
240 |
the input object.</p> |
|
|
241 |
|
|
|
242 |
<div id="syntax3" class="yui-syntax-highlight"><div class="numbers"><pre class="javascript" style="font-family:monospace;"><ol><li class="li1"><div class="de1"><span class="kw2">var</span> doCopy <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">// Create set4 with an object property 'obj'</span></div></li><li class="li1"><div class="de1"> <span class="kw2">var</span> set4 <span class="sy0">=</span> <span class="br0">{</span></div></li><li class="li2"><div class="de2"> obj<span class="sy0">:</span> <span class="br0">{</span><span class="br0">}</span></div></li><li class="li1"><div class="de1"> <span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">// Create a shallow copy of set4</span></div></li><li class="li1"><div class="de1"> <span class="kw2">var</span> copy <span class="sy0">=</span> Y.<span class="me1">merge</span><span class="br0">(</span>set4<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="co1">// Add a property to the copy inside of the 'obj' property</span></div></li><li class="li1"><div class="de1"> copy.<span class="me1">obj</span>.<span class="me1">addedToCopy</span> <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'After modifying the copy: '</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> </div></li><li class="li1"><div class="de1"> <span class="co1">// The result object is not the same as the original, but</span></div></li><li class="li1"><div class="de1"> <span class="kw2">var</span> msg <span class="sy0">=</span> <span class="br0">(</span><span class="st0">'"copy" should NOT be equal to the "original" (false expected): '</span> <span class="sy0">+</span> <span class="br0">(</span>copy <span class="sy0">===</span> set4<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> <span class="co1">// objects in the result object will reference the same object in</span></div></li><li class="li2"><div class="de2"> <span class="co1">// the input object.</span></div></li><li class="li1"><div class="de1"> msg <span class="sy0">+=</span> <span class="st0">'<br />copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): '</span> <span class="sy0">+</span> </div></li><li class="li1"><div class="de1"> <span class="br0">(</span>copy.<span class="me1">obj</span>.<span class="me1">addedToCopy</span> <span class="sy0">===</span> set4.<span class="me1">obj</span>.<span class="me1">addedToCopy</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li><li class="li1"><div class="de1"> Y.<span class="me1">log</span><span class="br0">(</span>msg<span class="br0">)</span><span class="sy0">;</span></div></li><li class="li2"><div class="de2"> result.<span class="me1">set</span><span class="br0">(</span><span class="st0">'innerHTML'</span><span class="sy0">,</span> <span class="st0">'<p>'</span> <span class="sy0">+</span> msg <span class="sy0">+</span> <span class="st0">'</p>'</span><span class="br0">)</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"><span class="br0">}</span><span class="sy0">;</span></div></li><li class="li1"><div class="de1"> </div></li></ol></pre></div><div class="nonumbers"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> doCopy <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> |
|
|
243 |
|
|
|
244 |
<span class="co1">// Create set4 with an object property 'obj'</span> |
|
|
245 |
<span class="kw2">var</span> set4 <span class="sy0">=</span> <span class="br0">{</span> |
|
|
246 |
obj<span class="sy0">:</span> <span class="br0">{</span><span class="br0">}</span> |
|
|
247 |
<span class="br0">}</span><span class="sy0">;</span> |
|
|
248 |
|
|
|
249 |
<span class="co1">// Create a shallow copy of set4</span> |
|
|
250 |
<span class="kw2">var</span> copy <span class="sy0">=</span> Y.<span class="me1">merge</span><span class="br0">(</span>set4<span class="br0">)</span><span class="sy0">;</span> |
|
|
251 |
|
|
|
252 |
<span class="co1">// Add a property to the copy inside of the 'obj' property</span> |
|
|
253 |
copy.<span class="me1">obj</span>.<span class="me1">addedToCopy</span> <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span> |
|
|
254 |
|
|
|
255 |
Y.<span class="me1">log</span><span class="br0">(</span><span class="st0">'After modifying the copy: '</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
256 |
|
|
|
257 |
<span class="co1">// The result object is not the same as the original, but</span> |
|
|
258 |
<span class="kw2">var</span> msg <span class="sy0">=</span> <span class="br0">(</span><span class="st0">'"copy" should NOT be equal to the "original" (false expected): '</span> <span class="sy0">+</span> <span class="br0">(</span>copy <span class="sy0">===</span> set4<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
259 |
|
|
|
260 |
<span class="co1">// objects in the result object will reference the same object in</span> |
|
|
261 |
<span class="co1">// the input object.</span> |
|
|
262 |
msg <span class="sy0">+=</span> <span class="st0">'<br />copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): '</span> <span class="sy0">+</span> |
|
|
263 |
<span class="br0">(</span>copy.<span class="me1">obj</span>.<span class="me1">addedToCopy</span> <span class="sy0">===</span> set4.<span class="me1">obj</span>.<span class="me1">addedToCopy</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
264 |
|
|
|
265 |
Y.<span class="me1">log</span><span class="br0">(</span>msg<span class="br0">)</span><span class="sy0">;</span> |
|
|
266 |
result.<span class="me1">set</span><span class="br0">(</span><span class="st0">'innerHTML'</span><span class="sy0">,</span> <span class="st0">'<p>'</span> <span class="sy0">+</span> msg <span class="sy0">+</span> <span class="st0">'</p>'</span><span class="br0">)</span><span class="sy0">;</span> |
|
|
267 |
<span class="br0">}</span><span class="sy0">;</span> |
|
|
268 |
</pre></div><textarea id="syntax3-plain">var doCopy = function () { |
|
|
269 |
|
|
|
270 |
// Create set4 with an object property 'obj' |
|
|
271 |
var set4 = { |
|
|
272 |
obj: {} |
|
|
273 |
}; |
|
|
274 |
|
|
|
275 |
// Create a shallow copy of set4 |
|
|
276 |
var copy = Y.merge(set4); |
|
|
277 |
|
|
|
278 |
// Add a property to the copy inside of the 'obj' property |
|
|
279 |
copy.obj.addedToCopy = true; |
|
|
280 |
|
|
|
281 |
Y.log('After modifying the copy: '); |
|
|
282 |
|
|
|
283 |
// The result object is not the same as the original, but |
|
|
284 |
var msg = ('"copy" should NOT be equal to the "original" (false expected): ' + (copy === set4)); |
|
|
285 |
|
|
|
286 |
// objects in the result object will reference the same object in |
|
|
287 |
// the input object. |
|
|
288 |
msg += '<br />copy.obj.addedToCopy should be equal to original.obj.addedToCopy (true expected): ' + |
|
|
289 |
(copy.obj.addedToCopy === set4.obj.addedToCopy); |
|
|
290 |
|
|
|
291 |
Y.log(msg); |
|
|
292 |
result.set('innerHTML', '<p>' + msg + '</p>'); |
|
|
293 |
}; |
|
|
294 |
</textarea></div> |
|
|
295 |
<p>See the <code>clone</code> method to create deep copies of objects.</p> |
|
|
296 |
</div> |
|
|
297 |
<div class="yui-u sidebar"> |
|
|
298 |
|
|
|
299 |
|
|
|
300 |
<div id="examples" class="mod box4"> |
|
|
301 |
<div class="hd"> |
|
|
302 |
<h4> |
|
|
303 |
The YUI Global Object Examples:</h4> |
|
|
304 |
</div> |
|
|
305 |
<div class="bd"> |
|
|
306 |
<ul> |
|
|
307 |
<li><a href='../yui/yui-core.html'>YUI Core</a></li><li><a href='../yui/yui-more.html'>Load All Modules</a></li><li><a href='../yui/yui-multi.html'>Multiple Instances</a></li><li><a href='../yui/yui-compat.html'>YUI 2.x and 3.x</a></li><li><a href='../yui/yui-loader-ext.html'>YUI Loader - Dynamically Adding YUI and External Modules</a></li><li><a href='../yui/yui-extend.html'>Create Class Hierarchies with <code>extend</code></a></li><li><a href='../yui/yui-augment.html'>Compose Classes of Objects with <code>augment</code></a></li><li><a href='../yui/yui-mix.html'>Add Behaviors to Objects with <code>mix</code></a></li><li class='selected'><a href='../yui/yui-merge.html'>Combine Data Sets with <code>merge</code></a></li><li><a href='../yui/yui-isa.html'>Check Data Types with <code>Lang</code></a></li><li><a href='../yui/yui-ua.html'>Browser Detection with <code>UA</code></a></li> </ul> |
|
|
308 |
</div> |
|
|
309 |
</div> |
|
|
310 |
|
|
|
311 |
<div class="mod box4"> |
|
|
312 |
<div class="hd"> |
|
|
313 |
<h4>More The YUI Global Object Resources:</h4> |
|
|
314 |
</div> |
|
|
315 |
<div class="bd"> |
|
|
316 |
<ul> |
|
|
317 |
<!-- <li><a href="http://developer.yahoo.com/yui/yui/">User's Guide</a> (external)</li> --> |
|
|
318 |
<li><a href="../../api/module_yui.html">API Documentation</a></li></ul> |
|
|
319 |
</div> |
|
|
320 |
</div> |
|
|
321 |
</div> |
|
|
322 |
</div> |
|
|
323 |
|
|
|
324 |
</div> |
|
|
325 |
</div> |
|
|
326 |
|
|
|
327 |
|
|
|
328 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
329 |
<!-- TABLE OF CONTENTS --> |
|
|
330 |
<div id="toc"> |
|
|
331 |
|
|
|
332 |
<ul> |
|
|
333 |
<li class="sect first">YUI 3 Resources</li><li class="item"><a title="YUI 3 -- Yahoo! User Interface (YUI) Library" href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="YUI 3 Dependency Configurator -- configure your custom YUI implementation" href="http://developer.yahoo.com/yui/3/configurator">YUI 3 Dependency Configurator</a></li><li class="item"><a title="The YUI 3 Forum on YUILibrary.com" href="http://yuilibrary.com/forum/viewforum.php?f=15">YUI 3 Forums (external)</a></li><li class="item"><a title="Found a bug or a missing feature? Let us know on YUILibrary.com." href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/license.html">YUI License</a></li><li class="item"><a title="Download and fork the YUI project on GitHub" href="http://github.com/yui">YUI on Github</a></li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="selected "><a title="YUI Global Object - Functional Examples" href="../../examples/yui/index.html">YUI Global Object</a></li><li class="item"><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="AsyncQueue - Functional Examples" href="../../examples/async-queue/index.html">AsyncQueue</a></li><li class="item"><a title="Browser History - Functional Examples" href="../../examples/history/index.html">Browser History</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Console Filters Plugin- Functional Examples" href="../../examples/console-filters/index.html">Plugin.ConsoleFilters <img src='http://l.yimg.com/a/i/not/beta_1.gif'></a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="item"><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">Other Useful YUI 3 Resources</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li></ul> |
|
|
334 |
</div> |
|
|
335 |
</div> |
|
|
336 |
</div><!--closes bd--> |
|
|
337 |
|
|
|
338 |
<div id="ft"> |
|
|
339 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
340 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
341 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
342 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
343 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
344 |
</div> |
|
|
345 |
</div> |
|
|
346 |
<script language="javascript"> |
|
|
347 |
var yuiConfig = {base:"../../build/", timeout: 10000, filter:"debug", logInclude: {example:true}}; |
|
|
348 |
</script> |
|
|
349 |
<script src="../../assets/syntax.js"></script> |
|
|
350 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
351 |
<script language="javascript"> |
|
|
352 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
353 |
</script> |
|
|
354 |
</body> |
|
|
355 |
</html> |