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

回答編集履歴

2

配列 -> Array#length / markdown修正

2019/05/14 13:48

投稿

think49
think49

スコア18194

answer CHANGED
@@ -114,10 +114,10 @@
114
114
  console.log(Object.getOwnPropertyDescriptor([], 'length')); // {value: 0, writable: true, enumerable: false, configurable: false}
115
115
  ```
116
116
 
117
- 配列がsetterのような挙動を示すのは、配列が**Exotic Objects**として内部メソッド `[[DefineOwnProperty]]` を書き換えている為です。
117
+ `Array#length` がsetterのような挙動を示すのは、配列が**Exotic Objects**として内部メソッド `[[DefineOwnProperty]]` を書き換えている為です。
118
118
 
119
119
  - [9.4.2 Array Exotic Objects - ECMAScript® 2018 Language Specification](http://www.ecma-international.org/ecma-262/9.0/#sec-array-exotic-objects)
120
- - [9.4.2.1 [[DefineOwnProperty]] - ECMAScript® 2018 Language Specification](http://www.ecma-international.org/ecma-262/9.0/#sec-array-exotic-objects)
120
+ - [9.4.2.1 DefineOwnProperty - ECMAScript® 2018 Language Specification](http://www.ecma-international.org/ecma-262/9.0/#sec-array-exotic-objects)
121
121
 
122
122
  ```JavaScript
123
123
  const array2 = [1,2,3,4,5,6,7,8,9];
@@ -126,7 +126,7 @@
126
126
  console.log(array2); // [1,2,3]; (4つ目以降の要素が削除されている)
127
127
  ```
128
128
 
129
- 配列はgetterでもありませんので、「要素数が確定するタイミング」は jQuery#length と基本的に同じです。
129
+ `Array#length` はgetterでもありませんので、「要素数が確定するタイミング」は jQuery#length と基本的に同じです。
130
130
  `Array#length` を参照した時に要素数を計算しているわけではなく、要素数の変動が起こる処理(Array#push, Array#filter等)の中で予め、length値を確定させています。
131
131
 
132
132
  ```JavaScript

1

arrayLike のsetterコード修正

2019/05/14 13:48

投稿

think49
think49

スコア18194

answer CHANGED
@@ -5,6 +5,7 @@
5
5
  ```JavaScript
6
6
  const arrayLike = {};
7
7
  Object.defineProperty(arrayLike, 'length', {
8
+ enumerable: true, configurable: true,
8
9
  get: function get () {
9
10
  return Object.keys(this).length;
10
11
  },
@@ -13,7 +14,7 @@
13
14
  length -= length % 1;
14
15
 
15
16
  for (let i of Object.keys(this)) {
16
- if (+i.toString() === i && i > length) {
17
+ if ((+i).toString() === i && i >= length) {
17
18
  delete this[i];
18
19
  }
19
20
  }