小编gre*_*hap的帖子

数组减少Polyfill说明

为冗长的帖子提前道歉.我想了解MDN提供的数组reduce polyfill.我无法理解polyfill中的一些行你能解释一下吗.下面是代码

    if (!Array.prototype.reduce) {
      Object.defineProperty(Array.prototype, 'reduce', {
        value: function(callback /*, initialValue*/) {
          if (this === null) {
            throw new TypeError( 'Array.prototype.reduce ' + 
              'called on null or undefined' );
          }
          if (typeof callback !== 'function') {
            throw new TypeError( callback +
              ' is not a function');
          }

          // 1. Let O be ? ToObject(this value).
          var o = Object(this);

          // 2. Let len be ? ToLength(? Get(O, "length")).
          var len = o.length >>> 0; 

          // Steps 3, 4, 5, …
Run Code Online (Sandbox Code Playgroud)

javascript arrays polyfills

4
推荐指数
1
解决办法
830
查看次数

标签 统计

arrays ×1

javascript ×1

polyfills ×1