为什么MDC原型函数以这种方式编写? [英] Why are the MDC prototype functions written this way?

查看:103
本文介绍了为什么MDC原型函数以这种方式编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MDC中,有大量代码片段意味着在不支持它们的浏览器中实现对新ECMAScript标准的支持,例如 Array.prototype.map 函数:

  if(!Array.prototype.map)
{
Array.prototype.map = 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)
抛出新的TypeError();

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

返回res;
};
}

使用此功能的好处是什么(如果有的话)而不是

  function(fun,thisp)
{
//相同的代码,只是没有var thisp = arguments [1]; line:
use strict;

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

var t = Object(this);
var len = t.length>>> 0;
if(typeof fun!==function)
抛出新的TypeError();

var res = new Array(len);
for(var i = 0; i< len; i ++)
{
if(i in t)
res [i] = fun.call(thisp,t [我,我,t);
}

返回res;
}

var t = Object(this); 而不是 var t = this; var len = t.length>>> 0; 而不是 var len = t.length;

解决方案

  var len = t.length>>> 0; 

这个问题基本上它确保数字为非负32位int。



对于Object构造函数:

  var t = Object(this) ; 

如果为null或未定义,它将返回一个空对象。来自 MDC对象文档


Object构造函数为给定值创建
对象包装器。如果
的值为null或未定义,则
将创建并返回一个空的
对象,否则,它将返回与
对应的
对象类型给定值。


它们只是快速纠正错误的方法。



编辑:我对thisp部分的思考太过分了。我假设使用arguments数组是一种确保参数默认为undefined的方法,但无论如何他们都是这样做的。 Mike Hofer在评论中说得对。这是Mozilla的编码风格,用于指示可选参数。如果将null或undefined作为第一个参数传入,则Function.call默认为global。来自关于Function.call的MDC文档


thisArg:
确定
fun中 this 的值。如果thisArg为null或未定义,则
将成为全局对象。
否则,将等于
Object(thisArg)(如果
thisArg已经是对象,则为thisArg,或
String,布尔值,如果thisArg

对应类型的原始值,则为Number。因此,当函数执行时,
始终是这个 ==
对象的类型。



In MDC there are plenty of code snippets that meant to implement support for new ECMAScript standards in browsers that don't support them, such as the Array.prototype.map function:

if (!Array.prototype.map)
{
  Array.prototype.map = 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 res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t)
        res[i] = fun.call(thisp, t[i], i, t);
    }

    return res;
  };
}

What's the benefit (if there's any) of using this function rather than

function(fun, thisp)
{
  // same code, just without the "var thisp = arguments[1];" line:
  "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 res = new Array(len);
  for (var i = 0; i < len; i++)
  {
    if (i in t)
      res[i] = fun.call(thisp, t[i], i, t);
  }

  return res;
}

, var t = Object(this); rather than var t = this; and var len = t.length >>> 0; rather than var len = t.length;?

解决方案

var len = t.length >>> 0;    

This was covered pretty well in this question Basically it makes sure the number is a non-negative 32 bit int.

As for the Object constructor:

var t = Object(this);

If this is null or undefined, it'll return an empty object. From the MDC Docs on Object

The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of type that corresponds to the given value.

They're both just quick ways to do error correction.

EDIT: I was thinking way too hard about the thisp part. I was assuming that using the arguments array was a way to ensure arguments were defaulting to undefined, but they do that on their own anyway. Mike Hofer got it right in the comments. It's Mozilla's coding style to indicate optional parameters. Function.call defaults to global if null or undefined is passed in as the first argument. From the MDC Docs on Function.call

thisArg: Determines the value of this inside fun. If thisArg is null or undefined, this will be the global object. Otherwise, this will be equal to Object(thisArg) (which is thisArg if thisArg is already an object, or a String, Boolean, or Number if thisArg is a primitive value of the corresponding type). Therefore, it is always true that typeof this == "object" when the function executes.

这篇关于为什么MDC原型函数以这种方式编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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