You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today, we learned the basic syntax for <strong>Javascript</strong>, the language used to <strong>animate</strong> the web. There are slides available, but most of what we did isn't on them. I'll do a brief summary here.
15
+
</p>
16
+
<p><strong>It's very important that you type all the lines of code, not copy paste!</strong> You learn best by doing, not by reading. The more you type, the more you'll retain, I promise.
17
+
</p>
18
+
<pclass="note">To run the code in this tutorial, you'll need to use Chrome's console (Command-Option-j on a Mac). If you're not using Chrome, quickly download it <ahref="https://www.google.com/intl/en/chrome/browser/" target="_blank">here</a>.
19
+
<p>
20
+
The astute will realize that the navigation of this tutorial is done with Javascript (notice that the URL never changes!). If you're curious, view the source and the script by looking at Resources tab in Chrome's Inspector (View -> Developer -> Inspect, click the 2nd tab, the arrow next to "index.html", then the arrow next to "scripts", then "main.js"), or just by opening it from the Day 5 folder in Sublime. <strong>You'll be able to understand most of it by the end of these 8 pages!</strong>
21
+
</p>
22
+
</div>
23
+
<divid="2" class="page">
24
+
<h1>Basic syntax</h1>
25
+
<p>A <strong>variable</strong> is exactly what it was in algebra: something in which to store data. In javascript, we declare and store variables like this:</p>
26
+
<code>
27
+
var a = 7; <spanclass="comment">// anything after 2 /'s is a <strong> comment </strong>and won't be executed.</span><br/>
28
+
<spanclass="comment">// so you can literally type anything!! a;lk ajd!</span>
29
+
30
+
</code>
31
+
<p>
32
+
The keyword <codeclass="inline">var</code> tells the browser to make a new variable called <codeclass="inline"> a</code>. The <codeclass="inline"> =</code> then tells the browser to set it to the value 7. Finally, the <codeclass="inline">;</code> tells the browser that the line of code is over -- <strong>All lines should end in semicolons!!</strong>
33
+
</p>
34
+
<p>
35
+
Then how come it returns <codeclass="inline"> undefined</code>? We'll get to that later. You can check what any variable is just by typing it:
36
+
</p>
37
+
<code>
38
+
a <br/>
39
+
<spanclass="comment"> // => 7 </span>
40
+
</code>
41
+
<pclass="note">I'll use the <codeclass="inline"><spanclass="comment">// =></span></code> to denote the <strong>output</strong> of a line of code -- no need to type it (or any other comments, for that matter) in.
42
+
</p>
43
+
44
+
</div>
45
+
<divid="3" class="page">
46
+
<h1>Types</h1>
47
+
<p>
48
+
There are (many) different types of variables. We'll learn about 3 today. A <strong>type</strong> tells the engine what kind of data a variable holds. For example, try this:
49
+
</p>
50
+
<code>
51
+
a = 7; <spanclass="comment">// We don't have to type 'var' again because the engine already knows a exists; var is for only the <strong>first time</strong> you declare a variable</strong></span><br>
52
+
var b = "7"; <br/>
53
+
a + b; <br/>
54
+
<spanclass="comment"> // => What will it be?</span>
55
+
</code>
56
+
<p>
57
+
You may be surprised to see it returned <codeclass="inline"> "77"</code>. That's because a is of type <strong>number</strong> and b is of type <strong>string</strong>, or a collection of letters (called <codeclass="inline"> chars</code> in comp sci talk). When you add two strings, you just concatenate them, as you'd expect (so "java"+"script" == "javascript"). The engine treated <codeclass="inline"> a</code> like a string so that the addition could make sense. How else would you add 7 to, say, "cat"?
58
+
</p>
59
+
<p>To check a variable's type:</p>
60
+
<code>
61
+
typeof a <spanclass="comment"> // => "number" </span><br/>
62
+
typeof b <spanclass="comment"> // => "string" </span><br/>
63
+
</code>
64
+
</div>
65
+
<divid="4" class="page">
66
+
<h1>Functions</h1>
67
+
<p>
68
+
<strong>Functions</strong> are also exactly what they were in algebra: something that takes input and gives output, and if the input is the same, the output is too. We write functions like this:
69
+
</p>
70
+
<pclass="note">To type a new line in the console, use <strong>shift + enter</strong></p>
71
+
72
+
<code>
73
+
function (a, b) { <br/>
74
+
<strong>return</strong> a + b; <br/>
75
+
}<br/>
76
+
</code>
77
+
<p>
78
+
This is correct syntax, but won't work in the console (something about an unexpected '('). We'll fix it in a minute, but first let's analyze what we typed. <codeclass="inline"> function</code> tells the browser that we're declaring a function. Everything enclosed in <codeclass="inline"> {}</code> is a part of the function: they tell the engine where the function starts and ends. <codeclass="inline"> (a, b)</code> defines the <strong>input</strong> -- in this function, we're taking two pieces of input, called <codeclass="inline"> a</code> and <codeclass="inline"> b</code>. Finally, the <codeclass="inline"> return</code> tells the function what to <strong> output</strong>, in this case, the sum of a and b.
79
+
</p>
80
+
</div>
81
+
<divid="5" class="page">
82
+
<h1>Making it work</h1>
83
+
<p>To make the code work, type this:</p>
84
+
85
+
<code>
86
+
var func = function (a, b) { <br/>
87
+
<strong>return</strong> a + b; <br/>
88
+
}<br/>
89
+
</code>
90
+
<p>
91
+
This is cool! We've just assigned the a function to the variable <codeclass="inline"> func</code>. To call (i.e. use) the function we use a similar syntax:
<spanclass="comment">// How about this one:</span><br/>
103
+
alert("howdy soldier!");
104
+
</code>
105
+
<p>
106
+
<codeclass="inline">alert</code> is that pesky function that some sites use to show messages. Now you know how to piss people off with javascript!</p>
107
+
<p>
108
+
Notice that javascript doesn't make you specify the <strong>types of input</strong>. That is, this will work too:
109
+
</p>
110
+
<code>
111
+
func("cat", "dog"); <spanclass="comment">// Love that show.</span><br/>
112
+
alert(17);<br/>
113
+
alert(func);<br/>
114
+
alert(func("cat", "dog")); <spanclass="comment">// Curveball! What will this do?
115
+
</code>
116
+
<p>
117
+
The last line is an example of a <strong>nested</strong> function. The engine evaluates the inner function, then uses its return value as the input to the outer function. Just like using <strong>f(g(x))</strong> back in Algebra!
118
+
</p>
119
+
120
+
</div>
121
+
<divid="6" class="page">
122
+
<h1>JS and the DOM</h1>
123
+
<p>You may be thinking, this is all very cool, but what does this have to do with the web? Well, Javascript has a special syntax for interacting with objects on the web page. Open up the console while viewing this page and type
124
+
</p>
125
+
<code>
126
+
document.get
127
+
</code>
128
+
<p>
129
+
Chrome should autofill with a lot of options, like <codeclass="inline"> getElementById</code> and <codeclass="inline"> getElementsByClassName</code>. Pick one and see what it does -- at this point, it should be pretty self explanatory. For example:
130
+
</p>
131
+
<code>
132
+
document.getElementById("6");<br/>
133
+
<spanclass="comment"> // => All the code for this page </span>
<pclass="note">To undo changes, all you have to do is refresh the page -- changes made with javascript don't <strong>persist</strong>, that is, they never change any of the original code that made a web page!</p>
143
+
<p>If the code isn't self-explanatory, don't worry: you will never again use it, because <strong>we have jQuery</strong>.
144
+
</p>
145
+
</div>
146
+
<divid="7" class="page">
147
+
<p>The above javascript is pretty cool, but it involves <strong>a lot of typing</strong>. And what if I didn't want to get <codeclass="inline"> #6</code> but something more complicated, like <codeclass="inline"> #footer a.some_class</code>? Then things get tough.
148
+
</p>
149
+
<h1>Enter jQuery</h1>
150
+
<p>jQuery makes all this easy. Instead of the annoying <codeclass="inline"> getElementById</code>, we can just use <strong>CSS selectors</strong> to get elements. Try:
151
+
</p>
152
+
<code>
153
+
$("#7"); <spanclass="comment"> // The same thing as document.getElementById("7") -- so much less typing! </span><br/>
154
+
$("code").css("color", "blue"); <br/>
155
+
$(".nav").html("javascript rocks"); <spanclass="comment"> // Check the next and previous buttons! </span><br/>
156
+
</code>
157
+
<p>
158
+
This is why we love jQuery, and it's <strong>essential</strong> that you know it as a web developer. No one (literally, no one) works in plain javascript anymore: everyone uses jQuery. Much, much more can be found at <ahref="http://jquery.com" target="_blank">jQuery.com</a>
159
+
</p>
160
+
<h1>I wanna do it myself!</h1>
161
+
<p>I'd hope so. Unfortunately, jQuery doesn't come built-in with the browser like javascript does. If you look at this page's source, you'll notice this in the head:
All you need to do is copy that into your own pages, and you have jQuery! To write javascript files (you don't want to be typing into the console all the time), make that <codeclass="inline"> scripts</code> folder in your project that we always talk about and never use, and create a main.js file in there.
168
+
</p>
169
+
</div>
170
+
<divid="8" class="page">
171
+
<h1>A working example</h1>
172
+
<p>Here is the example we did from class yesterday. First create a folder for the project (i.e. Day 5). In it, make an index.html. Make a stylesheets and scripts directory as usual. In the scripts folder, make a main.js.
173
+
</p>
174
+
<code>
175
+
<spanclass="comment"> // index.html </span><br/>
176
+
<!doctype html><br/>
177
+
<html><br/>
178
+
<head><br/>
179
+
<spanclass="comment"><!-- include jQuery -- ></span><Br/>
});<spanclass="comment"> // always remember to close your {}'s and ()'s!</span><br/>
198
+
<spanclass="comment"> // We'll go over what the {fontSize..} means later as well.
199
+
200
+
</code>
201
+
<p>
202
+
And that's it! Open up index.html in a browser and watch that text grow (the <codeclass="inline">1000</code> is the number of <strong>milliseconds</strong> the animation will take)!
203
+
</p>
204
+
<p>
205
+
Now you can use jQuery, CSS and HTML -- you <strong>know</strong> everything this class has to teach. All that's left is review, learning more functions and practice!
0 commit comments