Skip to content

Commit fd5f8a6

Browse files
committed
getter | setter function 🚀📝
1 parent cd51ef8 commit fd5f8a6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

01 - JavaScript-Basics/getter.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const obj = {
2+
log: ['a', 'b', 'c', 'd'],
3+
get latest() {
4+
if (this.log.length == 0) {
5+
return undefined;
6+
}
7+
return this.log[this.log.length - 1];
8+
}
9+
};
10+
11+
console.log(obj.latest);
12+
// expected output: "d"

01 - JavaScript-Basics/setter.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const language = {
2+
set current(name) {
3+
this.log.push(name);
4+
},
5+
log: []
6+
};
7+
8+
language.current = 'BN';
9+
language.current = 'EN';
10+
language.current = 'FA';
11+
language.current = 'ES';
12+
13+
console.log(language.log);
14+
// expected output: Array ["EN", "FA"]

0 commit comments

Comments
 (0)