-
Notifications
You must be signed in to change notification settings - Fork 2
/
foobar.js
87 lines (64 loc) · 1.28 KB
/
foobar.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
// variable
var num1 = 5
// function
var increment_by_five = function(num){
return num+5
}
var res = increment_by_five( num1 )
console.log('increment_by_five(): '+res)
// class
function IncrementClass(){
// set this object in current scope
var self = this
// property
self.num
// public method
self.setNum = function(num){
self.num = num
}
// public method
self.plusFive = function(){
doWork()
return this.num
}
// private method
function doWork(){
self.num = self.num+5
}
}
// construct an object
var incrementor = new IncrementClass()
//call objects public method
incrementor.setNum(5)
console.log('incrementor.num(): '+incrementor.num)
//call objects private method, through public method.
var res = incrementor.plusFive()
console.log('incrementor.plusFive() '+res)
/**
// function
var increment_by_five = function(num){
return num+5
}
// class
function IncrementClass(){
// property
this.num
// public
this.setNum = function(num){
this.num = num
}
// public
this.getResult = function(){
return increment()
}
// private
function increment(){
console.log(this.num)
return this.num+5
}
}
// object
var myObject = new IncrementClass()
myObject.setNum(234567890)
console.log( myObject.getResult() )
**/