【问题标题】:Array prototype method: Can `this` ever be `null`?数组原型方法:`this` 可以是 `null` 吗?
【发布时间】:2018-04-06 04:37:36
【问题描述】:

我在看Array.prototype.includes Polyfill, as shown on MDN
顶部有几行对我来说没有多大意义:

// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

具体来说,this == null 在那里检查。

this 怎么可能是null(甚至是假的)?这种情况怎么可能是真的?

据我所知,没有一个可以调用 .includes 的虚假值,因为即使是空数组也是真实的。
我确信null 支票的存在是有充分理由的,但它可能是什么?

spec linked in the polyfill 没有明确声明任何null 检查要求。


为了完整起见,这里是完整的 polyfill:

// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {

      // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

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

      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }

      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;

      // 5. If n ≥ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
      }

      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        // c. Increase k by 1. 
        if (sameValueZero(o[k], searchElement)) {
          return true;
        }
        k++;
      }

      // 8. Return false
      return false;
    }
  });
}

【问题讨论】:

    标签: javascript null prototype this polyfills


    【解决方案1】:

    我想我明白了

    (function() {
      "use strict";
    
      if (!Array.prototype.includes2) {
      Object.defineProperty(Array.prototype, 'includes2', {
        value: function(searchElement, fromIndex) {
    
          // 1. Let O be ? ToObject(this value).
          if (this == null) {
            throw new TypeError('"this" is null or not defined');
          }
    
          var o = Object(this);
    
          // 2. Let len be ? ToLength(? Get(O, "length")).
          var len = o.length >>> 0;
    
          // 3. If len is 0, return false.
          if (len === 0) {
            return false;
          }
    
          // 4. Let n be ? ToInteger(fromIndex).
          //    (If fromIndex is undefined, this step produces the value 0.)
          var n = fromIndex | 0;
    
          // 5. If n ≥ 0, then
          //  a. Let k be n.
          // 6. Else n < 0,
          //  a. Let k be len + n.
          //  b. If k < 0, let k be 0.
          var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
    
          function sameValueZero(x, y) {
            return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
          }
    
          // 7. Repeat, while k < len
          while (k < len) {
            // a. Let elementK be the result of ? Get(O, ! ToString(k)).
            // b. If SameValueZero(searchElement, elementK) is true, return true.
            // c. Increase k by 1. 
            if (sameValueZero(o[k], searchElement)) {
              return true;
            }
            k++;
          }
    
          // 8. Return false
          return false;
        }
      });
    }
    })();
    

    然后

    console.log(Array.prototype.includes2.call(null));
    // Uncaught TypeError: "this" is null or not defined
    

    MDN。严格模式。 “保护”JavaScript:(link)

    对于普通函数,this 始终是一个对象:无论是提供的 如果使用对象值 this 调用对象;值,装箱,如果 使用布尔值、字符串或数字 this 调用;或全局对象 if 使用未定义或 null this... 调用 因此,对于严格模式函数,指定的 this 不会被装箱 一个对象,如果未指定,this 将是未定义的。

    【讨论】:

    • 那么,“严格模式” 实现了这一点?你能告诉我为什么吗?
    • @Cerbrus 我用 MDN blockqute 更新了答案。
    • 啊,这就解释了发生了什么。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2021-12-24
    • 2011-07-30
    • 2015-05-06
    相关资源
    最近更新 更多