什么';javascript中对象(这个)的用途是什么?

什么';javascript中对象(这个)的用途是什么?,javascript,Javascript,研究MDNWeb文档中的find方法的polyfill,有一行我没有遵循,让我分享代码 if (!Array.prototype.find) { Object.defineProperty(Array.prototype, 'find', { value: function(predicate) { if (this == null) { throw TypeError('"this" is null or not defined'); }

研究MDNWeb文档中的find方法的polyfill,有一行我没有遵循,让我分享代码

if (!Array.prototype.find) {
  Object.defineProperty(Array.prototype, 'find', {
    value: function(predicate) {
      if (this == null) {
        throw TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      var len = o.length >>> 0;

      if (typeof predicate !== 'function') {
        throw TypeError('predicate must be a function');
      }

      var thisArg = arguments[1];

      var k = 0;

      while (k < len) {
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return kValue;
        }
        k++;
      }

      return undefined;
    },
    configurable: true,
    writable: true
  });
}

if(!Array.prototype.find){
Object.defineProperty(Array.prototype,“find”{
值:函数(谓词){
if(this==null){
抛出类型错误(“'this'为空或未定义”);
}
var o=对象(此);
var len=o.length>>>0;
if(类型谓词!=='function'){
抛出类型错误('谓词必须是函数');
}
var thisArg=参数[1];
var k=0;
while(k
我的问题是关于表达式
varo=Object(this)。这样做的目的是什么而不是做
var o=this
?。在上述两种情况下打印值都会返回相同的对象

这是调用
varo=newobject(this)的缩写方式吗

我已经删除了方法中的注释以缩短文本,下面是polyfill实现的链接


谢谢

在严格模式下,
这并不总是一个对象<代码>对象(此)
确保
o
是对象,而不是原语

以下是
作为原语的示例:

“严格使用”;
Object.defineProperty(String.prototype,“nishCap”{
可写:对,
对,,
值(){
console.log(此类型);/“字符串”
常数o=对象(此);
console.log(类型o);/“对象”
返回o.substring(0,1).toUpperCase()+o.substring(1);
}
});

const capped=“foo”.nishCap()
因为
Array.prototype.find
可以用
这个
值调用,该值不是对象。见:

调用find方法时,将执行以下步骤:

  • 让我们一起去吧?ToObject(此值)
  • 因此,为了完全符合规范,polyfill需要
    对象(this)
    。否则,实现将不同,正如您可以从以下两个代码段中看到的:

    “严格使用”;
    const str='abc';
    Array.prototype.find.call(
    str,
    (char,i,theThis)=>{
    //这应该是一个对象,即使在非对象上调用:
    控制台日志(theThis);
    }
    
    );
    您的答案在前面的注释中:
    //1。让我们一起去吧?ToObject(此值)。
    我必须熟悉规范文档中使用的语言。我知道我在这些评论中遗漏了一些东西,谢谢。@jcamejo-需要一些时间才能习惯!一件特别的事情是
    的使用在等级库操作之前(已描述)。这并不意味着否定。有时很难看穿它…:-)