forked from pearlchen/LLC-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_objects.html
372 lines (325 loc) · 13.3 KB
/
js_objects.html
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>
<meta charset="utf-8">
<title>Ladies Learning Code - JavaScript March 18, 2012</title>
<meta name="viewport" content="width=1280">
<!-- CoderDeck core and extension CSS files -->
<link rel="stylesheet" href="src/deck.js/core/deck.core.css" type="text/css">
<link rel="stylesheet" href="src/deck.js/extensions/navigation/deck.navigation.css">
<link rel="stylesheet" href="src/deck.js/extensions/status/deck.status.css">
<link rel="stylesheet" href="src/deck.js/extensions/hash/deck.hash.css">
<link rel="stylesheet" href="src/deck.js/extensions/menu/deck.menu.css">
<link rel="stylesheet" href="src/css/prettify.css">
<link rel="stylesheet" href="src/css/deck.coder.css">
<link rel="stylesheet" href="src/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="src/codemirror/theme/default.css">
<!-- Custom for LLC -->
<link href='http://fonts.googleapis.com/css?family=Istok+Web' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Podkova' rel='stylesheet' type='text/css'>
<link rel="stylesheet" id='style-theme-link' href="src/css/coderdeck.css" type="text/css" >
<style type="text/css"></style>
</head>
<body class="deck-container">
<script type='text/coderdeck' id='coderdeck-default'>
<html>
<head>
<script src='src/jquery.min.js'>SCRIPTEND</head>
<body>CODE</body>
</html>
</script>
<script type='text/coderdeck' id='coderdeck-style-example'>
<html>
<title>test</title>
<style>CODE</style>
<body>
</body>
</html>
</script>
<div id="presentation">
<!-- **************************************** -->
<article class='slide slide-subhead'>
<h1>JavaScript Part 4</h1>
<p>Objects</p>
</article>
<!-- SLIDE: -->
<article class='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>
<p class="centered"><img src="assets/bento_box_real.jpg" height="400" alt="A Real Bento Box" /></p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h3>How Bento Boxes are like Objects</h3>
<br><br><br><br>
<table width="100%">
<tr>
<td class="centered">
<img src="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>
<pre class="prettyprint">var takeout = "Fried Rice";</pre>
</td>
</tr>
<tr>
<td width="430" align="center" valign="middle">
<img src="assets/BentoBox.png" width="339" height="207" alt="Bento Box diagram" />
</td>
<td>
<p>An object holds a collection of values.</p>
<pre class="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: -->
<article class='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>
<p class="centered highlight"><code>var bentobox = <span class="assignment">{}</span>;</code></p>
<p class="centered sidenote">Yes, it's weird but think about the {} as a sort of funky container.</p>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h2>Object Properties</h2>
<p>The "compartments" of objects are called <span class="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>
<table width="100%" style="font-size: 24px;">
<tr>
<td width="42%">
<strong>Dot notation:</strong><br>
<code>myObject.property = value;</code>
<br><br>
<pre class="prettyprint">
bentobox.main = "Teriyaki";
bentobox.side = "Tempura";
</pre>
</td>
<td width="8%"></td>
<td width="50%">
<strong>Key-Value Lookup:</strong><br>
<code>myObject[key] = value;</code>
<br>
<br>
<pre class="prettyprint">
bentobox["main"] = "Teriyaki";
bentobox["side"] = "Tempura";
</pre>
<span class="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>
<pre class="prettyprint">
bentobox.soup0 = "Miso";
bentobox.soup1 = "Udon";</pre>
</td>
<td></td>
<td>
<pre class="prettyprint">
bentobox["soup"+1] = "Miso";
bentobox["soup"+2] = "Udon";
</pre></td>
</tr>
</table>
<br><br><br>
<p class="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: -->
<article class='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>
<table width="100%">
<tr>
<td class="centered"><img src="assets/BentoBox.png" width="339" height="207" alt="Bento box variation #1" /></td>
<td class="centered"><img src="assets/BentoBox2.png" width="339" height="207" alt="Bento box variation #2" /></td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<article class='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>
<p class="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>
<div class="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>
<p class="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: -->
<article class='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>
<textarea class='coder-editor'>SCRIPT
var firstName = "Pearl";
var numLetters = firstName.length;
document.write( "numLetters: " +
numLetters );
SCRIPTEND</textarea>
</article>
<!-- SLIDE: -->
<article class='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>
<table width="100%" style="font-size: 24px;">
<tr>
<td width="40%">
<strong>This function...</strong>
<pre class="prettyprint">
function sayHello(){
document.write("hello");
}
sayHello();
</pre>
</td>
<td width="5%" align="center"><br>
<br><br>
➞
</td>
<td>
<strong>is the same as:</strong>
<pre class="prettyprint">
sayHello = function() {
document.write("hello");
}
sayHello();
</pre>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td align="center">ↆ</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<strong>But now look at this!</strong>
<pre class="prettyprint">
var welcomeTeam = {};
welcomeTeam.sayHello = function() {
document.write("hello");
}
welcomeTeam.sayHello();
</pre>
</td>
</tr>
</table>
</article>
<!-- SLIDE: -->
<article class='slide slide-list'>
<h3>Object Methods</h3>
<p>When a function is associated with an object we call it a <span class="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>
<textarea class='coder-editor'>
<script>
var salePrice1 = 20.00 * 0.50;
var salePrice2 = 35.00 * 0.50;
var salePrice3 = 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>
<script type="coder-solution">SCRIPT
var salePrice1 = 20.00 * 0.50;
var salePrice2 = 35.00 * 0.50;
var salePrice3 = 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: -->
<article class='slide slide-list'>
<h3>Object Methods</h3>
<p>Here's another <span class="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>
<textarea class='coder-editor coder-editor-full'>
<script>
var emailTemplate = "Dear [First Name], thank you for subscribing.";
// uh-oh, let's make this more personal
document.write( emailTemplate );
</script></textarea>
<script type="coder-solution">SCRIPT
var emailTemplate = "Dear [First Name], thank you for subscribing.";
// uh-oh, let's make this more personal
var emailFinal = emailTemplate.replace("[First Name]", "Pearl")
document.write( emailFinal );
SCRIPTEND</script>
</article>
<!-- SLIDE: -->
<article class='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: <a href="http://www.scribd.com/doc/19457597/Javascript-Methods" target="_blank">http://www.scribd.com/doc/19457597/Javascript-Methods</a>.</p>
<p>Or this cheatsheet: <a href="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 -->
<article class='slide slide-list'>
<p class="centered vertically_centered"><a href="js_dom.html"><img src="assets/next_section_icon.gif" width="230" height="70" alt="Click to go to next section" /></a></p>
</article>
<!-- **************************************** -->
</div>
<!-- END div id=presentation -->
<script src='src/jquery.min.js'></script>
<script src="src/modernizr.js"></script>
<!-- Update these paths to point to the correct files. -->
<script src="src/jquery.tmpl.min.js"></script>
<script src="src/deck.js/core/deck.core.js"></script>
<!-- Code Mirror -->
<script src="src/codemirror/lib/codemirror.js"></script>
<script src="src/codemirror/mode/xml/xml.js"></script>
<script src="src/codemirror/mode/css/css.js"></script>
<script src="src/codemirror/mode/javascript/javascript.js"></script>
<script src="src/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<!-- Prettify -->
<script src="src/prettify.js"></script>
<!-- Deck Core and extensions -->
<script src="src/deck.js/extensions/status/deck.status.js"></script>
<script src="src/deck.js/extensions/navigation/deck.navigation.js"></script>
<script src='src/deck.coder.js'></script>
<script src="src/deck.js/extensions/hash/deck.hash.js"></script>
<script src="src/deck.js/extensions/menu/deck.menu.js"></script>
<script>
$(function() {
$.deck('.slide');
});
</script>
</body>
</html>