“var t = Object(this)”的重点是什么?在forEach的官方实施中? [英] What's the point of "var t = Object(this)" in the official implementation of forEach?

查看:142
本文介绍了“var t = Object(this)”的重点是什么?在forEach的官方实施中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据MDC ,ECMA-262,第5版给出了实施of forEach as:

According to the MDC, the ECMA-262, 5th edition gives the implementation of forEach as:

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t)
      fun.call(thisp, t[i], i, t);
    }
  };
}

任何人都可以告诉我var t = Object(this)这一行是什么是在做?对象(这个)与普通的不同之处是什么?在这里做差异的工作是什么?

Can anyone tell me what the line "var t = Object(this)" is doing? How does Object(this) differ from plain this? And what work is that difference doing here?

推荐答案

Mozilla实现只是试图模拟规范中描述的步骤, Object(this); 模拟第一步,调用 ToObject 内部方法:

The Mozilla implementations just try to emulate exactly the steps that are described in the specification, Object(this); emulates the first step, calling the ToObject internal method:

来自 Array.prototype.forEach 15.4.4.18


....

....

当使用$ b调用forEach方法时$ b一个或两个参数,以下
步骤:

When the forEach method is called with one or two arguments, the following steps are taken:


  1. 设O是调用$的结果b $ b ToObject将此值作为
    参数传递。

  1. Let O be the result of calling ToObject passing the this value as the argument.

设lenValue是调用[[Get]]内部方法的结果O的参数为length。

Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length".

设len为ToUint32(lenValue)。

Let len be ToUint32(lenValue).

....

致电作为函数的Object构造函数它在幕后执行类型转换,内部如 15.2.1.1 调用 ToObject 方法。

Calling the Object constructor as a function behind the scenes it performs type conversion, internally as described in 15.2.1.1 the ToObject method is called.

如果仔细观察,还有更多类似的事情,例如,行:

There are more things like this if you look carefully, for example, the line:

var len = t.length >>> 0;

他们正在模仿对 ToUint32 内部方法,如步骤3中所述,使用无符号右移运算符( >>> )。

They are emulating a call to the ToUint32 internal method, as described in the step 3, using the unsigned right shift operator (>>>).

编辑
之前的行回答为什么 Mozilla实现以这种方式执行。

The previous lines answer why the Mozilla implementation does it in this way.

您可能想知道ECMAScript规范的原因。需要调用 ToObject ,请回看第2步,这看起来很明显:

You might wonder why the ECMAScript spec. needs to call ToObject, check back the Step 2, and it will start to seem obvious:



  1. 让lenValue成为使用参数length调用O的[[Get]]内部方法的结果。


规范。需要确保在调用函数时使用的 this 值是一个对象,因为原始值没有任何内部方法,如你可以在第2步看到,需要 [[Get]](P)内部方法,以获得长度的值 property。

The spec. needs to ensure that the this value used when the function is called is an object, because primitive values don't have any internal methods, an as you can see on the step 2, the [[Get]](P) internal method is needed, to get the value of the length property.

这样做是因为严格函数(以及内置函数),你可以设置原始值作为函数的值,例如:

This is done because for strict functions (and also for built-in functions), you can set primitive values as the function's this value, e.g.:

(function () {"use strict"; return typeof this; }).call(5); // "number"

对于非严格函数, this value总是转换为Object:

While for non-strict functions, the this value is always converted to Object:

(function () { return typeof this; }).call(5); // "object"

这篇关于“var t = Object(this)”的重点是什么?在forEach的官方实施中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆