-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
103 lines (68 loc) · 1.44 KB
/
example.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
// example of private scoping
var animal = timber({
defaults: {
colour: 'red'
},
private: {
girlfriend: 'Amy'
},
init: function(name) {
console.log(name + ' is the colour ' + this.colour);
},
walk: function() {
console.log('I am walking with my girlfriend ' + this.private.girlfriend);
}
});
var myAnimal = new animal('Bob');
myAnimal.walk();
console.log('attempting to access private variables: ' + myAnimal.private);
/*
Bob is the colour red
I am walking with my girlfriend Amy
attempting to access private variables: undefined
*/
// example of default values and inheritence
var horse = animal.extend({
walk: function() {
this.super.walk();
console.log('really FAST!');
}
});
var myHorse = new horse('Greg');
myHorse.walk();
/*
Greg is the colour brown
I am walking with my girlfriend Amy
really FAST!
*/
// example of singleton classes, ref1 and ref2 point to the same instance of coolAPI
var coolAPI = timber({
singleton: true,
init: function() {
console.log('hello, I ran just once!');
}
});
var ref1 = coolAPI();
ref1.id = 5;
var ref2 = coolAPI();
console.log(ref2.id);
/*
hello, I ran just once!
5
*/
// example of variable binding
var person = timber({
defaults: {
location: 'California'
},
init: function() {
this.on('change:location', function(loc) {
console.log('now located in ' + loc);
});
}
});
var Sam = new person();
Sam.location = 'Nebraska';
/*
now located in Nebraska
*/