Skip to content

Commit bf51a77

Browse files
authored
Update mock_l7.js
1 parent c7c37f6 commit bf51a77

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

test/tool/mock_l7.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
if (!Array.prototype.at) {
2+
Object.defineProperty(Array.prototype, 'at', {
3+
value: function at(index) {
4+
// 1. 让 O 成为 ToObject(this)
5+
if (this == null) {
6+
throw new TypeError('Cannot convert undefined or null to object');
7+
}
8+
var O = Object(this);
9+
10+
// 2. 让 len 成为 ToLength(O.length)
11+
var len = O.length >>> 0;
12+
13+
// 3. 让 relativeIndex 成为 ToInteger(index)
14+
var relativeIndex = Math.trunc(index) || 0;
15+
16+
// 如果参数是 NaN,Math.trunc(NaN) 是 NaN,NaN || 0 会将其变成 0
17+
// 这符合标准(规范中 ToIntegerOrInfinity(undefined) 结果为 0)
18+
19+
// 4. 计算实际索引
20+
var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
21+
22+
// 5. 边界检查:如果超出范围,返回 undefined
23+
if (k < 0 || k >= len) {
24+
return undefined;
25+
}
26+
27+
// 6. 返回对应的属性值
28+
return O[k];
29+
},
30+
writable: true,
31+
configurable: true
32+
});
33+
}
134
class Event {
235
constructor() {
336
this.stacks = {};

0 commit comments

Comments
 (0)