回答編集履歴

4

markdown修正

2016/08/17 02:35

投稿

think49
think49

スコア18166

test CHANGED
@@ -1,3 +1,7 @@
1
+ ### Array.prototype.slice
2
+
3
+
4
+
1
5
  > list関数内にあるsliceメソッドのあとにチェーンされているcallはいったい何の意味があるのでしょうか。
2
6
 
3
7
 
@@ -12,7 +16,7 @@
12
16
 
13
17
 
14
18
 
15
- ---
19
+ ### Function.prototype.bind
16
20
 
17
21
 
18
22
 
@@ -56,21 +60,31 @@
56
60
 
57
61
 
58
62
 
63
+ - [19.2.3.2 Function.prototype.bind ( thisArg , ...args) – ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.bind)
64
+
65
+
66
+
67
+ ### Strict Mode と 非Strict Mode(Sloppy Mode)
68
+
69
+
70
+
59
71
  `this` 値の扱いにはStrict Modeと非Strict Mode(Sloppy Mode)で違いがあり、Strict Modeでは指定された値がそのまま使われますが、「非Strict Modeでは `this` 値に `Object` 型しか指定できない」という制約があります(ES3 仕様の名残です)。
60
72
 
61
73
  その為、非Strict Modeでは次の仕組みで `this` 値が決定されます。
62
74
 
63
75
 
64
76
 
65
- - `this` 値に `Object` 型が指定された場合 -> 指定された `Object` 型を `this` 値とする
77
+ - this 値に Object 型が指定された場合
66
78
 
67
- - `this` 値に `Object`, `Undefined`, `Null` 型以外が指定された場合 -> 指定された値を `Object`に変換し、`this` 値とする
79
+ -> 指定された Object 型this 値とする
68
80
 
69
- - `this` 値に `Undefined`, `Null` 型が指定された場合 -> 規定値であるグローバルオブジェクトを `this` 値とする(`undefined`, `null` は `Object` 型に変換できない)
81
+ - this 値に Object, Undefined, Null 型以外が指定された場合
70
82
 
83
+ -> 指定された値を **Object 型に変換**し、this 値とする
71
84
 
85
+ - this 値に Undefined, Null 型が指定された場合
72
86
 
73
- - [19.2.3.2 Function.prototype.bind ( thisArg , ...args) – ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.bind)
87
+ -> 規定値である**グローバルオブジェクト**を this 値とする(undefined, null は Object 型に変換できない)
74
88
 
75
89
 
76
90
 

3

Function#bindのコード追加

2016/08/17 02:35

投稿

think49
think49

スコア18166

test CHANGED
@@ -50,6 +50,8 @@
50
50
 
51
51
  list.bind('hoge')(1, 2, 3); // this -> "hoge" & arguments -> {"0":1,"1":2,"2":3}
52
52
 
53
+ list.bind(0, 37)(1, 2, 3); // this -> 0 & arguments -> {"0":37,"1":1,"2":2,"3":3}
54
+
53
55
  ```
54
56
 
55
57
 

2

Array#slice, Function#bind, this値の説明追加

2016/08/17 02:13

投稿

think49
think49

スコア18166

test CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
 
4
4
 
5
- `Array.prototype.slice` は `this` 値に対して処理するので疑似配列となる `arguments` を指定することで配列に変換してます。
5
+ `Array.prototype.slice` は `this` 値配列に変換(`ArraySpeciesCreate`)してから処理します。
6
+
7
+ 疑似配列となる `arguments` を `Function.prototype.call` の第一引数に指定し、第二引数以降を未指定にすることで配列変換処理だけを利用しています。
8
+
9
+
10
+
11
+ - [22.1.3.22 Array.prototype.slice (start, end) – ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.slice)
6
12
 
7
13
 
8
14
 
@@ -10,7 +16,7 @@
10
16
 
11
17
 
12
18
 
13
- > またlist3変数に代入するとき、list.bindを格納しているleadingThirtysevenListに1、2、3という三つの値を引数として受け取っていますが、
19
+ > またlist3変数に代入するとき、list.bindを格納しているleadingThirtysevenListに1、2、3という三つの値を引数として受け取っていますが、
14
20
 
15
21
  > 第一引数は、undefinedではないのでしょうか。
16
22
 
@@ -22,4 +28,48 @@
22
28
 
23
29
 
24
30
 
31
+ ```JavaScript
32
+
33
+ 'use strict';
34
+
35
+ function list () {
36
+
37
+ console.log('this -> '+ this);
38
+
39
+ console.log('arguments -> ' + JSON.stringify(arguments));
40
+
41
+ return Array.prototype.slice.call(arguments);
42
+
43
+ }
44
+
45
+
46
+
47
+ list(1, 2, 3); // this -> undefined & arguments -> {"0":1,"1":2,"2":3}
48
+
49
+ list.call(null, 1, 2, 3); // this -> null & arguments -> {"0":1,"1":2,"2":3}
50
+
51
+ list.bind('hoge')(1, 2, 3); // this -> "hoge" & arguments -> {"0":1,"1":2,"2":3}
52
+
53
+ ```
54
+
55
+
56
+
57
+ `this` 値の扱いにはStrict Modeと非Strict Mode(Sloppy Mode)で違いがあり、Strict Modeでは指定された値がそのまま使われますが、「非Strict Modeでは `this` 値に `Object` 型しか指定できない」という制約があります(ES3 仕様の名残です)。
58
+
59
+ その為、非Strict Modeでは次の仕組みで `this` 値が決定されます。
60
+
61
+
62
+
63
+ - `this` 値に `Object` 型が指定された場合 -> 指定された `Object` 型を `this` 値とする
64
+
65
+ - `this` 値に `Object`, `Undefined`, `Null` 型以外が指定された場合 -> 指定された値を `Object` 型に変換し、`this` 値とする
66
+
67
+ - `this` 値に `Undefined`, `Null` 型が指定された場合 -> 規定値であるグローバルオブジェクトを `this` 値とする(`undefined`, `null` は `Object` 型に変換できない)
68
+
69
+
70
+
71
+ - [19.2.3.2 Function.prototype.bind ( thisArg , ...args) – ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.bind)
72
+
73
+
74
+
25
75
  Re: aaaaaaaa さん

1

markdown修正

2016/08/17 02:07

投稿

think49
think49

スコア18166

test CHANGED
@@ -1,6 +1,12 @@
1
1
  > list関数内にあるsliceメソッドのあとにチェーンされているcallはいったい何の意味があるのでしょうか。
2
2
 
3
+
4
+
3
5
  `Array.prototype.slice` は `this` 値に対して処理するので疑似配列となる `arguments` を指定することで配列に変換しています。
6
+
7
+
8
+
9
+ ---
4
10
 
5
11
 
6
12