首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Array.prototype.find的实现中,为什么我们需要使用Objetct(this)来转换为对象?

在Array.prototype.find的实现中,为什么我们需要使用Objetct(this)来转换为对象?
EN

Stack Overflow用户
提问于 2021-04-04 19:53:53
回答 1查看 32关注 0票数 1

Array.prototype.find's implementation中,为什么我们需要使用Object(this)this转换为对象?

代码语言:javascript
复制
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
  Object.defineProperty(Array.prototype, 'find', {
    value: function(predicate) {
      // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw 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 IsCallable(predicate) is false, throw a TypeError exception.
      if (typeof predicate !== 'function') {
        throw TypeError('predicate must be a function');
      }

      // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
      var thisArg = arguments[1];

      // 5. Let k be 0.
      var k = 0;

      // 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return kValue.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return kValue;
        }
        // e. Increase k by 1.
        k++;
      }

      // 7. Return undefined.
      return undefined;
    },
    configurable: true,
    writable: true
  });
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-04 21:44:35

大多数情况下,是因为规范中这么写的。

但它为什么要这么做呢?因为稍后,它将访问值的属性(如.length[k]),该值必须是该值的对象。当然,polyfill中使用的成员访问语法已经为我们做了这件事,但在规范文本中,Get算法需要一个对象,到对象的转换应该只发生一次,而不是在每个成员访问时(尽管这将是不可区分的)。

为什么我们要将任意值转换为对象?因为可以使用原始值调用方法,就像this参数一样--这很少见,但也是可能的。正如说明书所说的

注意:find函数有意是泛型的;它不要求其this值必须是Array对象。因此,它可以被转移到其他类型的对象中作为方法使用。

举个例子:

代码语言:javascript
复制
String.prototype.find = Array.prototype.find;
console.log("test".find(x => x > 'f' && x < 't'))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66941001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档