回答編集履歴
2
スコープを調整
test
CHANGED
@@ -14,13 +14,29 @@
|
|
14
14
|
|
15
15
|
'use strict';
|
16
16
|
|
17
|
-
|
17
|
+
function eachfn (asset) {
|
18
18
|
|
19
|
-
|
19
|
+
console.log(this, asset);
|
20
|
+
|
21
|
+
}
|
20
22
|
|
21
23
|
|
22
24
|
|
25
|
+
function main () {
|
26
|
+
|
27
|
+
var assets = [1,2,3,4,5];
|
28
|
+
|
29
|
+
var trustId = 'trust';
|
30
|
+
|
31
|
+
|
32
|
+
|
23
|
-
assets.forEach(
|
33
|
+
assets.forEach(eachfn, trustId);
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
main();
|
24
40
|
|
25
41
|
```
|
26
42
|
|
@@ -42,13 +58,29 @@
|
|
42
58
|
|
43
59
|
'use strict';
|
44
60
|
|
45
|
-
|
61
|
+
function eachfn (trustId, asset) {
|
46
62
|
|
47
|
-
|
63
|
+
console.log(trustId, asset);
|
64
|
+
|
65
|
+
}
|
48
66
|
|
49
67
|
|
50
68
|
|
69
|
+
function main () {
|
70
|
+
|
71
|
+
var assets = [1,2,3,4,5];
|
72
|
+
|
73
|
+
var trustId = 'trust';
|
74
|
+
|
75
|
+
|
76
|
+
|
51
|
-
assets.forEach(f
|
77
|
+
assets.forEach(eachfn.bind(undefined, trustId));
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
main();
|
52
84
|
|
53
85
|
```
|
54
86
|
|
1
Function.prototype.bind
test
CHANGED
@@ -1,4 +1,56 @@
|
|
1
|
+
### Array.prototype.forEach()
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
第二引数でthis値
|
5
|
+
`Array.prototype.forEach` の第二引数で `this` 値を束縛可能です。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
- [Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#parameters)
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```JavaScript
|
14
|
+
|
15
|
+
'use strict';
|
16
|
+
|
17
|
+
var assets = [1,2,3,4,5];
|
18
|
+
|
19
|
+
var trustId = 'trust';
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
assets.forEach(function (asset) { console.log(this); }, trustId);
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
### Function.prototype.bind()
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
`Function.prototype.bind` で引数を束縛可能です。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
- [Function.prototype.bind() - JavaScript | MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
```JavaScript
|
42
|
+
|
43
|
+
'use strict';
|
44
|
+
|
45
|
+
var assets = [1,2,3,4,5];
|
46
|
+
|
47
|
+
var trustId = 'trust';
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
assets.forEach(function (trustId, asset) { console.log(trustId, asset); }.bind(null, trustId));
|
52
|
+
|
53
|
+
```
|
2
54
|
|
3
55
|
|
4
56
|
|