简体   繁体   English

为什么 Mozilla 将 Object.assign polyfill 放在匿名函数中?

[英]Why Mozilla put Object.assign polyfill in an anonymous function?

I have a question.我有个问题。

In the Polyfill section of MDN page for Object.assign function.MDN 页面的 Polyfill 部分中用于 Object.assign功能。

The technical writers in Mozilla put a polyfill of Object.assign in an anonymous function: Mozilla 的技术作者将Object.assign放入匿名函数中:

if (typeof Object.assign != 'function') {
  (function () {
    Object.assign = function (target) {
      ...
    };
  })(); // <-- anonymous function call
}

What is the different if we just put Object.assign = function (target) { directly inside the if statement without the anonymous function here?如果我们只是将Object.assign = function (target) {直接放在if语句中而不使用匿名函数,会有什么不同?

Why would they do that?他们为什么要那样做?

The technical writers in Mozilla Mozilla 中的技术作家

Not all editors of MDN are technical writers, it's an open wiki.并非 MDN 的所有编辑者都是技术作家,它是一个开放的 wiki。

Why would they put Object.assign polyfill in an anonymous function?他们为什么要将 Object.assign polyfill 放在匿名函数中?

There's absolutely no reason here.这里绝对没有理由。 It might have been useful to store static values, such as a reference to Object.prototype.hasOwnProperty in there, but that was not done so the IIFE was superfluous.存储静态值可能很有用,例如对Object.prototype.hasOwnProperty的引用在那里,但没有这样做,所以 IIFE 是多余的。 I've removed it . 我已经删除了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM