forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs101.js
181 lines (134 loc) · 3.85 KB
/
js101.js
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
/* logic */
var foo = true;
var bar = false;
// lots of things are truthy: '0', 'any string', [] (an empty array), {} (an empty object), any non-zero number
// lots of things are falsy: 0, '' (empty string), NaN, null, undefined
var baz = foo || bar || bim || baz; // logical OR operator || returns the value of the first true comparator (false if none)
var bim = foo && bar && bop && bip; // logical AND operator && returns the value of the last true comparator (false if none)
if (foo) {
// do something if foo is true
} else if (
bar && baz && bim
) {
// do something if bar is true AND foo was false
} else {
// do something if both foo and bar were false
}
/* strings and numbers */
var myNumber = '1';
var foo = parseInt(myNumber) + 2; // 3
var bar = 1 + 2; // 3
/* arrays */
var foo = ['apple', 'orange', 'banana'];
foo.length; // 3
foo[1]; // 'orange'
foo[5] = 'pear'; // assign an element to an index of the array
foo; // ['apple', 'orange', 'banana', undefined, undefined, 'pear']
foo.length; // 6 -- uh oh
var foo = ['apple', 'orange', 'banana'];
foo.push('pear'); // add to an array when last index is not known
foo.length; // 4 -- that's better
foo; // ['apple', 'orange', 'banana', 'pear']
var foo = {
bar : 'baz'
};
/* objects */
var myObject = {
'property' : 'value',
'method' : function() {
alert('hello');
return 'hello';
}
};
myObject.property; // 'value'
myObject.method(); // alerts 'hello'
/* functions */
// defining a function
function myFunction() {
// do stuff
};
var myFunction = function(a, b) {
// do stuff
return (a + b);
};
myFunction();
var foo = 'hello';
var myThing = (function() {
var myFn = function() {
alert(foo);
};
var foo = 'world';
return myFn;
})();
myThing();
// functions can be created and run without ever naming them
(function(arg) { alert(arg); })('hello'); // alerts 'hello'
(function($) {
jQuery('p').text('hello');
})(jQuery);
// functions can receive 0 or more arguments;
// these arguments can be anything, including other functions!
var myFunction = function(a, b) {
alert(a + ' ' + b);
};
var myFunction = function(fn) {
fn(); fn();
};
myFunction(function() { alert('hello'); }); // alerts 'hello' twice
// functions can return things
var myFunction = function(a, b) {
return a + ' ' + b;
};
// functions can even return other functions
var myFunction = function(a, b) {
var foo = function() {
alert(a);
alert(b);
};
return foo;
};
var f = myFunction('hello', 'world');
f(); // alerts 'hello', then alerts 'world'
// functions "close" their variables -- variables defined in a function
// are only available to other objects and functions created inside the function
var myFunction = function() {
var myClosedVariable = 'hello';
};
myFunction();
alert(myClosedVariable); // error; myClosedVariable isn't defined outside the function
var myFunction = function() {
function myFn() {
alert(myClosedVariable);
}
var myClosedVariable = 'hello';
return myFn;
};
alert(myClosedVariable); // error; myClosedVariable isn't defined outside the function
var f = myFunction(); // returns the function created inside myFunction
f(); // this function still has access to myClosedVariable,
// because the function and the variable
// were created in the same scope. this is an example of a closure.
/* another closure example */
// this won't work as expected! all will alert '4'
for (i=0; i<5; i++) {
$('<p>some text</p>').appendTo('body').click(function() {
alert(i);
});
}
// this will work -- put the variable i inside a closure
for (i=0; i<5; i++) {
(function(i){
$('<p>some text</p>')
.appendTo('body').click(function() {
alert(i);
});
})(i);
}
// this will also work, and is a little more clear about what's going on
var createFunction = function(i) {
return function() { alert(i); };
};
for (i=0; i<5; i++) {
$('<p>some text</p>')
.appendTo('body').click(createFunction(i));
}