我不知道Object(this)的意思 [英] I have no idea Object(this) means

查看:219
本文介绍了我不知道Object(this)的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://developer.mozilla.org / en / docs / Web / JavaScript / Reference / Global_Objects / Array / fill

有一行像

// Steps 1-2.
if (this == null) {
  throw new TypeError('this is null or not defined');
}

var O = Object(this);          // <- WHAT IS THIS???????????

// Steps 3-5.
var len = O.length >>> 0;

// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;

// Step 8.
var k = relativeStart < 0 ?
  Math.max(len + relativeStart, 0) :
  Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
  len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ?
  Math.max(len + relativeEnd, 0) :
  Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
  O[k] = value;
  k++;
}

// Step 13.
return O;

我找不到将O指定为对象的必要性(这个)。

and I can't find any necessity to assign O as Object(this).

这是为了便于阅读还是有任何具体的分配理由?

Is it written just for readability or is there any specific reason for assigning?

推荐答案

正如对代码的评论中所建议的那样,本节将准确地填写规范

As suggested in the comments on the code, this section is to accurately pollyfill the first steps documented in the spec.



  1. 让O成为ToObject(这个值)。

  2. ReturnIfAbrupt(O)。


虽然有点无序,但这是执行 ToObject(此值)的功能

Though a bit out-of-order, this is performing the fucntion of ToObject(this value):

var O = Object(this);

基本上,如果在非对象上调用它,则应将非对象强制转换为非对象 Object

Basically, if it is called on a non-object, the non-object should be cast to an Object.

例如,如果我们在JavaScript引擎中运行这个大部分无意义的代码,本机支持此方法,我们会看到返回 Number 对象实例。

For example, if we were to run this bit of mostly-nonsensical code in a JavaScript engine which natively supports this method, we would see a Number object instance gets returned.

Array.prototype.fill.call(123);

该行将确保polyfill获得相同的结果。

That line would ensure the same result from the polyfill.

这篇关于我不知道Object(this)的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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