forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs101.js
391 lines (296 loc) · 7.4 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* FUNCTIONS
**/
/**
* Functions create closures; variables defined
* inside of them are accessible inside the
* function, and to any functions that were
* defined within the same scope.
**/
$(document).ready(function() {
var sayItOutside = (function(){
var myVar = 'hello world',
sayIt = function() {
console.log(myVar);
};
myVar = 'a new value';
return sayIt;
}());
// sayItOutside(); // 'a new value'
// console.log(myVar);
});
/**
* Functions are first class objects; we can move
* them around just like we do with other types
* of objects.
**/
$(document).ready(function() {
$('#myDiv').click(function(e) {
// console.log(e.target);
});
var repeater = function(fn, repeat) {
while (repeat--) { fn(); }
};
// repeater(function() { console.log('hello'); }, 5);
});
/**
* Functions can create other functions.
**/
$(document).ready(function() {
var makeRepeater = function(fn, repeat) {
return function() {
while (repeat--) {
fn();
}
};
};
var newFunction = makeRepeater(function() { console.log('hello'); }, 5);
newFunction();
});
/**
* Inside a function, you have access to 'arguments',
* an array-like object that contains a list of the
* arguments passed to the function.
**/
$(document).ready(function() {
$('#myDiv').one('click', function() {
// console.log(arguments);
});
});
/**
* OBJECTS
**/
/**
* Objects can have properties and methods.
**/
$(document).ready(function() {
var myObj = {
paul : 'Google',
adam : 'Bocoup',
alex : 'BazaarVoice',
sayHi : function() {
console.log('hello');
}
};
});
/**
* Inside an object method, 'this' refers to
* the object that owns the method by default.
**/
$(document).ready(function() {
var myObj = {
paul : 'Google',
adam : 'Bocoup',
alex : 'BazaarVoice',
sayHi : function(person) {
var company = this[person] || 'no one';
console.log(
'Hello,',
person,
', you work for',
company,
'.'
);
}
};
// myObj.sayHi('paul');
});
/**
* The module pattern is the goodness of closures
* combined with the goodness of objects.
**/
$(document).ready(function() {
var myModule = (function(){
// set up all the variables you'll need for
// the module's functionality
var privateVar = 'secret',
publicVar = 'not secret',
tellSecret = function() {
console.log(privateVar);
},
setSecret = function(str) {
if (str.length < 6) {
console.log('not secret enough!');
return;
}
privateVar = str;
};
// decide what to expose as a public API
// to the module.
return {
tellSecret : tellSecret,
setSecret : setSecret,
publicVar : publicVar
};
}());
// myModule.setSecret('new');
});
/**
* Objects can share functionality by sharing
* a "prototype" -- in fact, jQuery.fn is just
* shorthand for jQuery.prototype, which allows
* all of the methods available on jQuery objects
* to be shared across objects.
*/
$(document).ready(function() {
var Person = function(first, last) {
/**
* Setting instance properties
**/
this.firstName = first;
this.lastName = last;
return this;
};
Person.prototype = {
sayFirstName : function() {
console.log(this.firstName);
}
};
/**
* Creating "instances" of Person
**/
var adam = new Person('adam', 'sontag'),
rebecca = new Person('rebecca', 'murphey');
// adam.sayFirstName();
// rebecca.sayFirstName();
});
/**
* Methods can be changed on individual instances
* without affecting other instances.
**/
$(document).ready(function() {
var Person = function(first, last) {
this.firstName = first;
this.lastName = last;
return this;
};
Person.prototype = {
sayFirstName : function() {
console.log(this.firstName);
}
};
var adam = new Person('adam', 'sontag'),
rebecca = new Person('rebecca', 'murphey');
/**
* Changing the definition of an instance method
**/
// rebecca.sayFirstName = function() {};
// rebecca.sayFirstName();
// adam.sayFirstName();
});
/**
* ... Unless you want to affect other instances! In
* which case, you should modify the prototype instead.
**/
$(document).ready(function() {
var Person = function(first, last) {
this.firstName = first;
this.lastName = last;
return this;
};
Person.prototype = {
sayFirstName : function() {
console.log(this.firstName);
}
};
var adam = new Person('adam', 'sontag'),
rebecca = new Person('rebecca', 'murphey');
/**
* Changing the definition of a protype method
**/
Person.prototype.sayFirstName = function() {
console.log('i have no name');
};
// rebecca.sayFirstName();
// adam.sayFirstName();
});
/**
* We can also create new objects that share the same
* prototype using Object.create, which can be created
* where it is not available.
**/
$(document).ready(function() {
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var Person = {
sayName : function() {
console.log(this.firstName, this.lastName);
}
};
var adam = Object.create(Person);
$.extend(adam, {
firstName : 'adam',
lastName : 'sontag'
});
// console.log('adam is', adam);
});
/**
* CONTEXT
**/
/**
* We can change the meaning of 'this' inside
* of a function. In fact, jQuery does exactly
* that in order to have 'this' refer to the
* element that triggered an event handler.
*
* To change the context in which a function runs --
* that is, to change the meaning of 'this',
* we use .call() or .apply().
**/
$(document).ready(function() {
var p = {
firstName : 'paul',
lastName : 'irish'
},
a = {
firstName : 'alex',
lastName : 'sexton'
},
sayHi = function(greeting, question) {
console.log(greeting, this.firstName, this.lastName, question);
};
/**
* .apply takes a scope object and an array of arguments
* to pass to the function that is being called. The
* arguments in the array will be passed to the function
* as individual arguments.
**/
// sayHi.apply(p, [ 'Hello', 'How are you today?' ]);
/**
* .call takes a scope object and zero or more additonal
* arguments. Additonal arguments will be passed to the
* function as individual arguments.
**/
// sayHi.call(a, 'Greetings', 'Is it so just cloud?' );
});
/**
* jQuery offers the utility method $.proxy, which
* lets you pass a function and the scope in which
* the function should run.
*
* Be careful, though: the order of the arguments
* passed to $.proxy can be confusing.
*
* $.proxy(context, 'methodName');
* $.proxy(function() { ... }, context);
*
* This means we can easily change the meaning of this
* inside an event handler, where this normally refers
* to the element to which the handler was bound.
**/
$(document).ready(function() {
var myObj = {
thingToSay : 'it worked!',
sayIt : function() {
console.log('The thing I want to say is', this.thingToSay);
}
};
// myObj.sayIt();
// $('#myDiv').click(myObj.sayIt);
// $('#myDiv').click($.proxy(myObj, 'sayIt'));
});