teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

Array#lengthのmarkdownをJavaScriptコードブロックにした

2015/10/20 03:57

投稿

think49
think49

スコア18194

answer CHANGED
@@ -17,7 +17,7 @@
17
17
 
18
18
  `Function#length` は公開されていますが、`length` プロパティは読み取り専用の為、不正に書き換わることはありません。
19
19
 
20
- ```
20
+ ```JavaScript
21
21
  function sum (a, b) { return a + b; }
22
22
  sum.length = 3; // TypeError: Cannot assign to read only property 'length' of function sum(a, b) { return a + b; }
23
23
  console.log(sum.length); // 2

1

Function#length のサンプルコードで sum.length = 3; で正常処理するよう書いていたが、実際には TypeError が発生していたので修正

2015/10/20 03:57

投稿

think49
think49

スコア18194

answer CHANGED
@@ -19,9 +19,9 @@
19
19
 
20
20
  ```
21
21
  function sum (a, b) { return a + b; }
22
- sum.length = 3;
22
+ sum.length = 3; // TypeError: Cannot assign to read only property 'length' of function sum(a, b) { return a + b; }
23
- console.log(sum.length; // 2
23
+ console.log(sum.length); // 2
24
- console.log(Object.getOwnPropertyDescriptor(array, 'length')); // {value: 1, writable: true, enumerable: false, configurable: false}
24
+ console.log(Object.getOwnPropertyDescriptor(sum, 'length')); // Object {value: 2, writable: false, enumerable: false, configurable: true}
25
25
  ```
26
26
 
27
27
  `Array.prototype` を拡張すると `for-in` で拡張した `Array.prototype` 上のプロパティが列挙される(いわゆる `Array.prototype` 汚染)がありますが、既存の `Array.prototype.slice` 等は `for-in` で列挙されません。