-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathgetters-setters.js
70 lines (64 loc) · 1.85 KB
/
getters-setters.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
/**
* Getters and Setters
*
* Getters and setters have been available in Chrome since day one, in Firefox since version 2,
* version 3 of Safari, Internet Explorer 9 and up, and in all mobile browsers.
*
* @Reference
* http://engineering.wix.com/2015/04/21/javascript-the-extra-good-parts/
* http://javascriptplayground.com/blog/2013/12/es5-getters-setters/
*/
// Getters and Setters as explicit methods
(function () {
function wrapValue(value) {
return {
getValue: function () {
return value;
},
setValue: function (newValue) {
value = newValue;
}
};
}
var x = wrapValue(5);
console.log(x.getValue()); // output 5
x.setValue(7);
console.log(x.getValue()); // output 7
})();
// Using getters and setters -- Conventional way
(function () {
function wrapValue(_value) {
return {
get value() {
return _value;
},
set value(newValue) {
_value = newValue;
}
};
}
var x = wrapValue(5);
console.log(x.value); // output 5
x.value = 7;
console.log(x.value); // output 7
})();
// Using getters and setters -- Using Object.defineProperty
// When you define a property this way, you can do much more than just define a setter or getter. You may also pass following keys:
// configurable (false by default): if this is true, the property's configuration will be modifiable in future.
// enumerable (false by default): if true, the property will appear when looping over the object (for (var key in obj)).
(function() {
var person = {
firstName: 'Jimmy',
lastName: 'Smith'
};
Object.defineProperty(person, 'fullName', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var words = name.split(' ');
this.firstName = words[0] || '';
this.lastName = words[1] || '';
}
});
})();