javascript - js怎么查看一些方法的实现源代码 比如数组的push()方法
天蓬老师
天蓬老师 2017-04-10 16:52:16
[JavaScript讨论组]

js怎么查看一些方法的实现源代码 比如数组的push()方法 可以查看到吗

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
ringa_lee

Just for fun :)

V8:
https://github.com/v8/v8/blob/master/src/js/array.js

// Appends the arguments to the end of the array and returns the new
// length of the array. See ECMA-262, section 15.4.4.7.
function ArrayPush() {
  CHECK_OBJECT_COERCIBLE(this, "Array.prototype.push");

  if (%IsObserved(this))
    return ObservedArrayPush.apply(this, arguments);

  var array = TO_OBJECT(this);
  var n = TO_LENGTH_OR_UINT32(array.length);
  var m = %_ArgumentsLength();

  // It appears that there is no enforced, absolute limit on the number of
  // arguments, but it would surely blow the stack to use 2**30 or more.
  // To avoid integer overflow, do the comparison to the max safe integer
  // after subtracting 2**30 from both sides. (2**31 would seem like a
  // natural value, but it is negative in JS, and 2**32 is 1.)
  if (m > (1 << 30) || (n - (1 << 30)) + m > kMaxSafeInteger - (1 << 30)) {
    throw MakeTypeError(kPushPastSafeLength, m, n);
  }

  for (var i = 0; i < m; i++) {
    array[i+n] = %_Arguments(i);
  }

  var new_length = n + m;
  array.length = new_length;
  return new_length;
}
PHP中文网

原生实现的方法看不了的~~~

巴扎黑

不同的JavaScript引擎实现是不同的,而且可能是不同语言写的。

不过,对于Chrome浏览器或者Node.js,它们用的v8,你可以看这里:

https://github.com/v8/v8

首先声明,我没有看过。不过我找到了这个目录,里面有一些是用JavaScript写的:

https://github.com/v8/v8/tree/master/src/js

你可以去研究一下。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号