File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
134class Event {
235 constructor ( ) {
336 this . stacks = { } ;
You can’t perform that action at this time.
0 commit comments