回答編集履歴

3

ES5 \(Strict Mode\) ではブロックレベル関数宣言が `SyntaxError` になる

2016/04/21 17:29

投稿

think49
think49

スコア18156

test CHANGED
@@ -104,7 +104,7 @@
104
104
 
105
105
 
106
106
 
107
- ### ブロックレベル関数宣言(block-level FunctionDeclaration)の巻き上げ
107
+ ### [ES6] ブロックレベル関数宣言(block-level FunctionDeclaration)
108
108
 
109
109
 
110
110
 
@@ -180,7 +180,7 @@
180
180
 
181
181
 
182
182
 
183
- **(余談)**
183
+ **FunctionStatement**
184
184
 
185
185
  @nanto_vi さんは `if` 文内で条件付関数宣言する動作が Firefox 独自拡張だった頃に便宜上の名称として `FunctionStatement` (function 文) と説明しました。
186
186
 
@@ -192,12 +192,46 @@
192
192
 
193
193
 
194
194
 
195
+ ### [ES5] ブロックレベル関数宣言で SyntaxError
196
+
197
+
198
+
199
+ ES5 の Strcit Mode では**ブロックレベル関数宣言は SyntaxError を返す仕様**でした。
200
+
201
+ 「Windows Vista のサポート終了日(2017/04/11)」まではブロックレベル関数宣言の利用を控えた方がいいかもしれません。
202
+
203
+
204
+
205
+ - [Where's Walden? » New ES5 strict mode requirement: function statements not at top level of a program or function are prohibited](http://whereswalden.com/2011/01/24/new-es5-strict-mode-requirement-function-statements-not-at-top-level-of-a-program-or-function-are-prohibited/)
206
+
207
+ - [Windows デスクトップ製品のライフサイクル](https://www.microsoft.com/ja-jp/windows/lifecycle/default.aspx?navIndex=4#vista)
208
+
209
+
210
+
211
+ ```JavaScript
212
+
213
+ 'use strict';
214
+
215
+
216
+
217
+ if (true) {
218
+
219
+ function blockedDeclaration () {} // SyntaxError
220
+
221
+ }
222
+
223
+ ```
224
+
225
+
226
+
195
227
  ### 更新履歴
196
228
 
197
229
 
198
230
 
199
231
  - 2016/04/22 01:55 ブロックレベル関数宣言(block-level FunctionDeclaration)の説明追加
200
232
 
233
+ - 2016/04/22 02:25 ES5 (Strict Mode) ではブロックレベル関数宣言が `SyntaxError` になる
234
+
201
235
 
202
236
 
203
237
  Re: aaaaaaaa さん

2

ブロックスコープな関数宣言\(FunctionDeclaration\)の巻き上げの説明追加\(ES6 規定\)

2016/04/21 17:29

投稿

think49
think49

スコア18156

test CHANGED
@@ -8,6 +8,10 @@
8
8
 
9
9
 
10
10
 
11
+ - [13.3.2 Variable Statement - ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-variable-statement)
12
+
13
+
14
+
11
15
  ```JavaScript
12
16
 
13
17
  'use strict';
@@ -40,6 +44,10 @@
40
44
 
41
45
 
42
46
 
47
+ - [13.3.1 Let and Const Declarations - ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations)
48
+
49
+
50
+
43
51
  ```JavaScript
44
52
 
45
53
  'use strict';
@@ -72,6 +80,10 @@
72
80
 
73
81
 
74
82
 
83
+ - [14.1 Function Definitions - ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions)
84
+
85
+
86
+
75
87
  ```JavaScript
76
88
 
77
89
  'use strict';
@@ -92,4 +104,100 @@
92
104
 
93
105
 
94
106
 
107
+ ### ブロックレベル関数宣言(block-level FunctionDeclaration)の巻き上げ
108
+
109
+
110
+
111
+ ES6 から **Strict Mode 限定**でブロックスコープで巻き上げられる関数宣言を定義出来るようになりました(注: IE10- で未対応です)。
112
+
113
+
114
+
115
+ - [9.2.12 FunctionDeclarationInstantiation(func, argumentsList) – ECMA-262 6th Edition](http://www.ecma-international.org/ecma-262/6.0/#sec-functiondeclarationinstantiation)
116
+
117
+ - [block-level function declaration - ECMAScript 6 compatibility table](http://kangax.github.io/compat-table/es6/#test-block-level_function_declaration)
118
+
119
+
120
+
121
+ ```JavaScript
122
+
123
+ 'use strict';
124
+
125
+ console.log(blockedDeclaration); // ReferenceError: blockedDeclaration is not defined
126
+
127
+
128
+
129
+ /**
130
+
131
+ * 関数宣言(Strict Mode ではブロックスコープ)
132
+
133
+ */
134
+
135
+ if (true) {
136
+
137
+ console.log(blockedDeclaration); // function blockedDeclaration() {}
138
+
139
+ function blockedDeclaration () {}
140
+
141
+ }
142
+
143
+
144
+
145
+ console.log(blockedDeclaration); // ReferenceError: blockedDeclaration is not defined
146
+
147
+ ```
148
+
149
+
150
+
151
+ 「非Strict Mode (sloppy mode)」では関数スコープで巻き上げられます。
152
+
153
+
154
+
155
+ ```JavaScript
156
+
157
+ console.log(blockedDeclaration); // undefined
158
+
159
+
160
+
161
+ /**
162
+
163
+ * 関数宣言(sloppy mode では関数スコープ)
164
+
165
+ */
166
+
167
+ if (true) {
168
+
169
+ console.log(blockedDeclaration); // function blockedDeclaration() {}
170
+
171
+ function blockedDeclaration () {}
172
+
173
+ }
174
+
175
+
176
+
177
+ console.log(blockedDeclaration); // function blockedDeclaration() {}
178
+
179
+ ```
180
+
181
+
182
+
183
+ **(余談)**
184
+
185
+ @nanto_vi さんは `if` 文内で条件付関数宣言する動作が Firefox 独自拡張だった頃に便宜上の名称として `FunctionStatement` (function 文) と説明しました。
186
+
187
+ 便宜上の名称なので仕様上は「文」ではないのですが、ブロックスコープ単位で評価するこの動作は `Statement` に性質が近いので言い得て妙だと思います。
188
+
189
+
190
+
191
+ - [Function Expression Statements: Days on the Moon](http://nanto.asablo.jp/blog/2005/12/10/172622)
192
+
193
+
194
+
195
+ ### 更新履歴
196
+
197
+
198
+
199
+ - 2016/04/22 01:55 ブロックレベル関数宣言(block-level FunctionDeclaration)の説明追加
200
+
201
+
202
+
95
203
  Re: aaaaaaaa さん

1

表現の微修正

2016/04/21 16:56

投稿

think49
think49

スコア18156

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
 
34
34
 
35
- ### 関数式(LexicalDeclaration)
35
+ ### let, const 文は巻き上がらない
36
36
 
37
37
 
38
38
 
@@ -64,7 +64,7 @@
64
64
 
65
65
 
66
66
 
67
- ### 関数宣言(FunctionDeclaration)
67
+ ### 関数宣言(FunctionDeclaration)の巻き上げ
68
68
 
69
69
 
70
70