最近同事分享了一道题
复制代码
1
2
3
4
5
6
7
8
9
10let obj = { 2:3, 3:4, length:2, push:Array.prototype.push } obj.push(1) obj.push(2) console.log(obj)
obj是一个类数组,其中引入了Array原型链上的push方法,关键就是要搞清楚Array.prototype.push的方法定义。
经过查看资料,发现push的方法可以用js如下模拟:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30Array.prototype.push = function(...items) { let O = Object(this); // ecma 中提到的先转换为对象 let len = this.length >>> 0; let argCount = items.length >>> 0; // 2 ^ 53 - 1 为JS能表示的最大正整数 if (len + argCount > 2 ** 53 - 1) { throw new TypeError("The number of array is over the max value") } for(let i = 0; i < argCount; i++) { O[len + i] = items[i]; } let newLength = len + argCount; O.length = newLength; return newLength; }
push的参数可以是一个元素,或者多个逗号连接的元素。
在这道题中,
复制代码
1
2
3obj.push(1)相当于obj[obj.length] = 1,obj.length = obj.length+1; obj.push(2)相当于obj[obj.length] = 2,obj.length = obj.length+1;
因此最后的结果是
复制代码
1
2
3console.log(obj) {2:1,3:2,length:4,push:fn}
如果改成
复制代码
1
2
3
4
5
6
7
8
9let obj = { 2:3, 3:4, length:2 } Array.prototype.push.call(obj,1) Array.prototype.push.call(obj,2) console.log(obj)
结果是相同地,使用call将push方法借用给obj对象
最后
以上就是阔达大门最近收集整理的关于由一道题看push方法的实现原理的全部内容,更多相关由一道题看push方法内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复