Skip to content

Commit d26239e

Browse files
committed
Add class and object with methods
1 parent dd7356c commit d26239e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

JavaScript/2-middle.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const adder = initial => ({
4+
value: initial,
5+
add(value) {
6+
this.value += value;
7+
return this;
8+
}
9+
});
10+
11+
const Adder = class {
12+
constructor(initial) {
13+
this.value = initial;
14+
}
15+
add(value) {
16+
this.value += value;
17+
return this;
18+
}
19+
};
20+
21+
// Usage
22+
23+
{
24+
const v = new Adder(3).add(-9).add(12);
25+
console.log(v);
26+
}
27+
28+
{
29+
const v = adder(3).add(-9).add(12);
30+
console.log(v);
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy