-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_34.js
More file actions
61 lines (58 loc) · 2.37 KB
/
item_34.js
File metadata and controls
61 lines (58 loc) · 2.37 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
// Store methods on Prototypes
// We could have easily defined the User class from 'Item 30' as follows
function User(name, passwordHash) {
this.name = name;
this.passwordHash = passwordHash;
this.toString = function () {
return "[User " + this.name + "]";
};
this.checkPassword = function(password) {
return hash(password) === this.passwordHash;
};
}
// if we created 3 user instances
var u1 = User("u1", "ZXCVB");
var u2 = User("u2", "UIOP");
var u3 = User("u3", "QWERTY");
/* Instead of sharing the 'toString' and 'checkPassword' methods via the
* 'prototype', each instance contains a copy of both methods for a total of
* six function objects
*
* Storing methods on instance objects
* -----------------------------------
*
*
* User.prototype
* ----------------------
* | |
* --------| ---------------------- |-----------
* | | |
* | | |
* prototype prototype prototype
* | | |
* ------------------ ------------------ ----------------
* | .toString | | .toString | | .toString |
* | .checkPassword| | .checkPassword| | .checkPassword|
* | .name | | .name | | .name |
* | .passwordHash | | .passwordHash | | .passwordHash |
* ------------------ ------------------ ------------------
*
*
* Storing methods on a prototype object
* -------------------------------------
*
*
* User.prototype
* ----------------------
* | .toString |
* | .checkPassword |
* --------| ---------------------- |-----------
* | | |
* | | |
* prototype prototype prototype
* | | |
* ------------------ ------------------ ----------------
* | .name | | .name | | .name |
* | .passwordHash | | .passwordHash | | .passwordHash |
* ------------------ ------------------ ------------------
*/