-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter4.coffee
79 lines (65 loc) · 1.57 KB
/
chapter4.coffee
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
console.log "================================================================================"
console.log "Chapter 4"
console.log "========================================"
console.log "Question 1"
root = global ? window
root.aphorism = 'Fool me 8 or more times, shame on me'
do restoreOldAphormism = ->
aphorism = 'Fool me once, shame on you'
console.log aphorism
console.log aphorism
# fix
do restoreOldAphormism = ->
root.aphorism = 'Fool me once, shame on you'
console.log aphorism
console.log aphorism
console.log "========================================"
console.log "Question 2"
Genie = ->
Genie::wishesLeft = 3
Genie::grantWish = ->
if @wishesLeft > 0
console.log 'Your wish is granted!'
@wishesLeft--
else
console.log "You don't have more wish"
g1 = new Genie
g1.grantWish()
g1.grantWish()
g1.grantWish()
g1.grantWish()
g2 = new Genie
g2.grantWish()
g2.grantWish()
g2.grantWish()
g2.grantWish()
# fix
GenieFix = ->
GenieFix.wishesLeft = 3
GenieFix::grantWish = ->
if GenieFix.wishesLeft > 0
console.log 'Your wish is granted!'
GenieFix.wishesLeft--
else
console.log "You don't have more wish"
g1 = new GenieFix
g1.grantWish()
g1.grantWish()
g1.grantWish()
g1.grantWish()
g2 = new GenieFix
g2.grantWish()
g2.grantWish()
g2.grantWish()
g2.grantWish()
console.log "========================================"
console.log "Question 4"
(window ? global).property = 'global context'
@property = 'surrounding context'
class Foo
constructor: -> @property = 'instance context'
bar: -> console.log @property
foo = new Foo
bar = foo.bar
foo.bar()
bar()