-
Notifications
You must be signed in to change notification settings - Fork 154
/
02-custom-type-example.js
53 lines (42 loc) · 999 Bytes
/
02-custom-type-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
/*
* User.js
*/
// Private
var userCount = 0;
function depositeMinusFee(num1) {
return num1 - 0.1;
}
// Public
module.exports = User;
function User(n) {
this.id = userCount;
this.name = n;
this._paid = false;
this.balance = 0;
userCount++;
}
User.prototype.togglePaid = function() {
this._paid = !this._paid;
};
User.prototype.userType = function() {
if(this._paid) return 'Paid User';
else return 'Free User';
};
User.prototype.addBalance = function(amount) {
this.balance += depositeMinusFee(amount);
};
/*
* app.js
*/
var User = require('./User');
var bob = new User('Bob');
var joe = new User('Joe');
console.log(bob.id); // 0
console.log(joe.id); // 1
console.log(bob.balance); // 0
bob.addBalance(100);
console.log(bob.balance); // 99.9
console.log(bob._paid); // false (_paid is private; DON'T DO THIS!)
bob.togglePaid();
console.log(bob.userType()); // 'Paid User'
console.log(joe.userType()); // 'Free User'