-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamples.js
More file actions
193 lines (145 loc) · 3.49 KB
/
samples.js
File metadata and controls
193 lines (145 loc) · 3.49 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
$(function(){
var presentation = {
scoping : function(){
function doSomething(){
myVar = "foo"; //this goes into the global scope which == bad
var myOtherVar = "bar"; //this goes into the function scope which == good
}
doSomething();
console.log(myVar); //foo
console.log(myOtherVar); //error
},
andor3 : function(arg){
var myFunc = function(arg){
var myVal = document.getElementById(arg) ||
document.createElement("div"); //if no arg was passed in myVal is "defaultValue"
console.log(myVal);
};
myFunc();
myFunc("andor3");
},
args : function(){
var myFunc = function(){
console.log(arguments);
};
myFunc("Hello", "World", function(){});
},
closure : (function(){
var total = 0;
return function(){
total++;
console.log(total);
};
}()),
this1 : function(){
console.log( this );
window.myFunc();
},
this2 : function(){
var person = new Person("Bart");
},
this3 : function(){
myObj = {
name : "Homer",
sayHello : function(){
//look what happens when this is used inside a nested function
return (function(){
console.log("Hello, " + this.name);
console.log(this);
}());
}
};
myObj.sayHello();
},
inheritance1 : function(){
var Car = function(){ };
//add properties to Car's Prototype
Car.prototype = {
wheels : 4, doors : 4, manufacturer : ""
};
var Fusion = function(){ };
//set the Fusion's prototype to a new instance of car
Fusion.prototype = new Car();
Fusion.prototype.constructor = Fusion; //reset the R8's constructor
var fusion = new Fusion();
console.log(fusion);
console.log(fusion.wheels);
},
optionsPattern : function(){
$(document).myPlugin({color:'black', height: '500px'});
$(document).myPlugin();
},
pubSub : function(){
var contacts = {
collection : ['Aaron','Drew','Tom'],
update : function(){
this.collection[0] = 'Arian';
this.collection[1] = 'Adrian';
this.collection[2] = 'Ray';
$(this).trigger('change');
}
};
$(contacts).on('change',function(){
var contactSelect = $('select[name="contact"]');
contactSelect.find('option').remove();
for(var i = 0; i < contacts.collection.length; i++){
contactSelect.append('<option value="">' + contacts.collection[i] + '</option>');
}
});
contacts.update();
},
extendPattern : function(){
var mammal = {
hair : "brown",
breath : function(){}
};
var cat = $.extend(mammal,{
eyes: 2,
ears: 2
});
console.log(cat);
}
};
$(document).on('click','input[type="button"]',function(){
var $this = $(this);
if(presentation[$this.data('run')]){
presentation[$this.data('run')]($this.data('args'));
}
});
});
//intentional globals
var myFunc = function(){
console.log(this);
};
var obj = {
init : function(){
$('#thisButton').on('click',function(){
console.log(this);
});
}
};
obj.init();
var obj1 = {
init : function(){
var self = this; //create a ref to the current "this" scope
$("#thisButton1").on('click',function(){
console.log(self);
});
}
};
obj1.init();
function Person(name){
this.name = name;
console.log(this);
console.log(this.name);
}
$.fn.myPlugin = function(options){
var defaults = {color: "blue", height: "200px" };
//remember to check the type of the argument!
if(typeof options === 'object'){
options = $.extend(defaults,options);
} else {
options = defaults;
}
console.log(options);
};