This repository was archived by the owner on Dec 9, 2018. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathjs_objects.html
More file actions
Latest commit
372 lines (325 loc) · 13.3 KB
/
Copy pathjs_objects.html
File metadata and controls
372 lines (325 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<title>Ladies Learning Code - JavaScript March 18, 2012</title>
<metaname="viewport" content="width=1280">
<!-- CoderDeck core and extension CSS files -->
<linkrel="stylesheet" href="src/deck.js/core/deck.core.css" type="text/css">
<linkrel="stylesheet" href="src/deck.js/extensions/navigation/deck.navigation.css">
<linkrel="stylesheet" href="src/deck.js/extensions/status/deck.status.css">
<linkrel="stylesheet" href="src/deck.js/extensions/hash/deck.hash.css">
<linkrel="stylesheet" href="src/deck.js/extensions/menu/deck.menu.css">
<linkrel="stylesheet" href="src/css/prettify.css">
<linkrel="stylesheet" href="src/css/deck.coder.css">
<linkrel="stylesheet" href="src/codemirror/lib/codemirror.css">
<linkrel="stylesheet" href="src/codemirror/theme/default.css">
<!-- Custom for LLC -->
<linkhref='http://fonts.googleapis.com/css?family=Istok+Web' rel='stylesheet' type='text/css'>
<linkhref='http://fonts.googleapis.com/css?family=Podkova' rel='stylesheet' type='text/css'>
<linkrel="stylesheet" id='style-theme-link' href="src/css/coderdeck.css" type="text/css" >
<styletype="text/css"></style>
</head>
<bodyclass="deck-container">
<scripttype='text/coderdeck' id='coderdeck-default'>
<html>
<head>
<scriptsrc='src/jquery.min.js'>SCRIPTEND</head>
<body>CODE</body>
</html>
</script>
<scripttype='text/coderdeck' id='coderdeck-style-example'>
<html>
<title>test</title>
<style>CODE</style>
<body>
</body>
</html>
</script>
<divid="presentation">
<!-- **************************************** -->
<articleclass='slide slide-subhead'>
<h1>JavaScript Part 4</h1>
<p>Objects</p>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Objects</h2>
<p>If variables are boxes, OBJECTS can be thought of as bento boxes -- "advanced variables" if you will.</p>
<pclass="centered"><imgsrc="assets/bento_box_real.jpg" height="400" alt="A Real Bento Box" /></p>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h3>How Bento Boxes are like Objects</h3>
<br><br><br><br>
<tablewidth="100%">
<tr>
<tdclass="centered">
<imgsrc="assets/Rice.png" width="193" height="119" alt="Rice box diagram" />
<br><br><br>
</td>
<td>
<p>A basic variable only holds one value.</p>
<preclass="prettyprint">var takeout = "Fried Rice";</pre>
</td>
</tr>
<tr>
<tdwidth="430" align="center" valign="middle">
<imgsrc="assets/BentoBox.png" width="339" height="207" alt="Bento Box diagram" />
</td>
<td>
<p>An object holds a collection of values.</p>
<preclass="prettyprint">// create a new object
var bentobox = {};
// fill it with stuff
bentobox.main = "Teriyaki";
bentobox.side = "Tempura";
bentobox.salad = "Seaweed Salad";
bentobox.soup = "Miso";
bentobox.sauce = "Soya";
bentobox.dessert = "Fruit";</pre>
</td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Creating an Object</h2>
<p>Creating an object is easy. Just like any other variable, use the <strong><code>var</code></strong> keyword and give it a descriptive name. </p>
<p>But instead of assigning a single value right away, use these curly braces to make an empty object.</p>
<pclass="centered highlight"><code>var bentobox = <spanclass="assignment">{}</span>;</code></p>
<pclass="centered sidenote">Yes, it's weird but think about the {} as a sort of funky container.</p>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Object Properties</h2>
<p>The "compartments" of objects are called <spanclass="keyword">PROPERTIES</span>.</p>
<p>If you already have an existing object, there are a few ways to access and set a value to a property. Since these two are very commonly used, they will both be discussed.</p>
<tablewidth="100%" style="font-size: 24px;">
<tr>
<tdwidth="42%">
<strong>Dot notation:</strong><br>
<code>myObject.property = value;</code>
<br><br>
<preclass="prettyprint">
bentobox.main = "Teriyaki";
bentobox.side = "Tempura";
</pre>
</td>
<tdwidth="8%"></td>
<tdwidth="50%">
<strong>Key-Value Lookup:</strong><br>
<code>myObject[key] = value;</code>
<br>
<br>
<preclass="prettyprint">
bentobox["main"] = "Teriyaki";
bentobox["side"] = "Tempura";
</pre>
<spanclass="sidenote">This will be most useful after you understand arrays. <strong>Advantage</strong>:
You can use concatenation with the key string in order to do look up something that's partially dynamic:</span>
</td>
</tr>
<tr>
<td>
<preclass="prettyprint">
bentobox.soup0 = "Miso";
bentobox.soup1 = "Udon";</pre>
</td>
<td></td>
<td>
<preclass="prettyprint">
bentobox["soup"+1] = "Miso";
bentobox["soup"+2] = "Udon";
</pre></td>
</tr>
</table>
<br><br><br>
<pclass="sidenote"><strong>Advanced sidenote: </strong><br>
Alternatively, you can both declare an object and set it's values at the <strong>same</strong> time: <br>
<code>var bentobox = { soup:"Miso", main:"Teriyaki" };</code></p>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Why use Objects?</h2>
<p><em>Advanced topic:</em> Objects are useful because you group all these properties together and you can <strong>reuse</strong> the object over again.</p>
<p>There's an <strong>expectation</strong> of what you'll get in a bento box. <br>
<br>
Some items are <strong>fixed</strong> (a fruit dessert, a miso soup) but some compartments can be <strong>edited</strong> (a gyoza side versus tempura). </p>
<tablewidth="100%">
<tr>
<tdclass="centered"><imgsrc="assets/BentoBox.png" width="339" height="207" alt="Bento box variation #1" /></td>
<tdclass="centered"><imgsrc="assets/BentoBox2.png" width="339" height="207" alt="Bento box variation #2" /></td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Think of Everything as an Object*</h2>
<p>When I said that objects were "advanced variables", here's another way of looking at it:</p>
<pclass="centered"><strong>All JavaScript variables can be treated like objects too.</strong></p>
<div>
<p>If curly braces are a container for a generic object, quotes are containers for characters. When we get to arrays next, you'll see that it uses square brackets. (The only "uncontainered" object we've introduced today was a number.)</p>
</div>
<divclass="reminders">
<pre>var stringObject = “ ”;
var arrayObject = [ ];</pre>
</div>
<p>Some objects are simpler than others but almost everything in JavaScript has properties available to us.</p>
<pclass="sidenote">*Again, I need to oversimplify. The concept called DATA TYPE is a hairy topic. For more info: http://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ and http://www.2ality.com/2011/03/javascript-values-not-everything-is.html</p>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Built-in JavaScript Properties</h2>
<p>In this workshop we won't be creating any of our own objects but JavaScript already has many objects built into the language.</p>
<p>Let's look at a string object and the <code>length</code> property which returns the number of characters in a string. Try putting your own name in here.</p>
<textareaclass='coder-editor'>SCRIPT
var firstName = "Pearl";
var numLetters = firstName.length;
document.write( "numLetters: " +
numLetters );
SCRIPTEND</textarea>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Functions in Objects</h2>
<p>Previously, we talked about functions as blocks of re-usable code. This might sound crazy but <strong>you can store functions in variables</strong> too!</p>
<tablewidth="100%" style="font-size: 24px;">
<tr>
<tdwidth="40%">
<strong>This function...</strong>
<preclass="prettyprint">
function sayHello(){
document.write("hello");
}
sayHello();
</pre>
</td>
<tdwidth="5%" align="center"><br>
<br><br>
➞
</td>
<td>
<strong>is the same as:</strong>
<preclass="prettyprint">
sayHello = function() {
document.write("hello");
}
sayHello();
</pre>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<tdalign="center">ↆ</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<strong>But now look at this!</strong>
<preclass="prettyprint">
var welcomeTeam = {};
welcomeTeam.sayHello = function() {
document.write("hello");
}
welcomeTeam.sayHello();
</pre>
</td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h3>Object Methods</h3>
<p>When a function is associated with an object we call it a <spanclass="keyword">METHOD</span>.</p>
<p>Let's say you're having a robot sale! Everything is 50% off. But, after doing the math, you notice that the sale price is a bit off.. Let's declare a sale price and manipulate it using <code>toFixed()</code>. When you use a method, it gives you access to all the hard work that someone else did and it only takes you one line to use it!</p>
<textareaclass='coder-editor'>
<script>
varsalePrice1=20.00*0.50;
varsalePrice2=35.00*0.50;
varsalePrice3=25.99*0.50;
// uh-oh, can we make sure we
// always see 2 decimal spaces?
document.write(salePrice1);
document.write("<br />");
document.write(salePrice2);
document.write("<br />");
document.write(salePrice3);
</script></textarea>
<scripttype="coder-solution">SCRIPT
varsalePrice1=20.00*0.50;
varsalePrice2=35.00*0.50;
varsalePrice3=25.99*0.50;
// uh-oh, can we make sure we
// always see 2 decimal spaces?
document.write(salePrice1.toFixed(2));
document.write("<br />");
document.write(salePrice2.toFixed(2));
document.write("<br />");
document.write(salePrice3.toFixed(2));
SCRIPTEND</script>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h3>Object Methods</h3>
<p>Here's another <spanclass="keyword">METHOD</span> associated with strings.</p>
<p>Have you ever created an email newsletter? You usually have a template but you want to replace <code>[First Name]</code> with someone's actual name. Use <code>.replace()</code> for that!</p>
<textareaclass='coder-editor coder-editor-full'>
<script>
varemailTemplate="Dear [First Name], thank you for subscribing.";
// uh-oh, let's make this more personal
document.write(emailTemplate);
</script></textarea>
<scripttype="coder-solution">SCRIPT
varemailTemplate="Dear [First Name], thank you for subscribing.";
// uh-oh, let's make this more personal
varemailFinal=emailTemplate.replace("[First Name]","Pearl")
document.write(emailFinal);
SCRIPTEND</script>
</article>
<!-- SLIDE: -->
<articleclass='slide slide-list'>
<h2>Looking up properties and references</h2>
<p><code>length</code> and <code>toFixed()</code> are only two of many (many!) more built-in properties and methods of objects in JavaScript.</p>
<p>Check out this list: <ahref="http://www.scribd.com/doc/19457597/Javascript-Methods" target="_blank">http://www.scribd.com/doc/19457597/Javascript-Methods</a>.</p>
<p>Or this cheatsheet: <ahref="http://www.addedbytes.com/download/javascript-cheat-sheet-v1/png/" target="_blank">http://www.addedbytes.com/download/javascript-cheat-sheet-v1/png</a>.</p>
</article>
<!-- SLIDE: Next section -->
<articleclass='slide slide-list'>
<pclass="centered vertically_centered"><ahref="js_dom.html"><imgsrc="assets/next_section_icon.gif" width="230" height="70" alt="Click to go to next section" /></a></p>
</article>
<!-- **************************************** -->
</div>
<!-- END div id=presentation -->
<scriptsrc='src/jquery.min.js'></script>
<scriptsrc="src/modernizr.js"></script>
<!-- Update these paths to point to the correct files. -->
<scriptsrc="src/jquery.tmpl.min.js"></script>
<scriptsrc="src/deck.js/core/deck.core.js"></script>
<!-- Code Mirror -->
<scriptsrc="src/codemirror/lib/codemirror.js"></script>
<scriptsrc="src/codemirror/mode/xml/xml.js"></script>
<scriptsrc="src/codemirror/mode/css/css.js"></script>
<scriptsrc="src/codemirror/mode/javascript/javascript.js"></script>
<scriptsrc="src/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<!-- Prettify -->
<scriptsrc="src/prettify.js"></script>
<!-- Deck Core and extensions -->
<scriptsrc="src/deck.js/extensions/status/deck.status.js"></script>
<scriptsrc="src/deck.js/extensions/navigation/deck.navigation.js"></script>
<scriptsrc='src/deck.coder.js'></script>
<scriptsrc="src/deck.js/extensions/hash/deck.hash.js"></script>
<scriptsrc="src/deck.js/extensions/menu/deck.menu.js"></script>
<script>
$(function(){
$.deck('.slide');
});
</script>
</body>
</html>