├── index.html
└── src
└── js
└── index.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
8 | HW11-js-this
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/js/index.js:
--------------------------------------------------------------------------------
1 | console.log('------------- #3')
2 | let counter = {
3 | i: 0,
4 | up: function () {
5 | this.i += 1
6 | },
7 | down: function () {
8 | this.i -= 1
9 | },
10 | showStep: function () {
11 | return this.i
12 | }
13 | }
14 |
15 | counter.up();
16 | counter.up();
17 | console.log(counter.showStep())
18 |
19 | console.log('------------- #4')
20 | function Calculator() {
21 | this.sum = function (a) {
22 | let result = a
23 | return function (b) {
24 | return result += b
25 | }
26 | }
27 | this.multiply = function (a) {
28 | let result = a
29 | return function (b) {
30 | return result *= b
31 | }
32 | }
33 | this.subtract = function (a) {
34 | let result = a
35 | return function (b) {
36 | return result -= b
37 | }
38 | }
39 | this.divide = function (a) {
40 | let result = a
41 | return function (b) {
42 | return result /= b
43 | }
44 | }
45 | }
46 | let calculator = new Calculator();
47 | console.log(calculator.sum(1)(2));
48 | console.log(calculator.multiply(2)(2));
49 | console.log(calculator.subtract(4)(2));
50 | console.log(calculator.divide(8)(2));
--------------------------------------------------------------------------------