Internet Explorer 11 是否仍需要 ES5 shim?

Is ES5 shim still needed for Internet Explorer 11?

我正在查看 compat tables,IE 11 似乎支持所有 ES5,除了(在杂项下)此功能:"Enumerable properties can be shadowed by non-enumerables"。尽管提供了示例,但我不明白这意味着什么。

我可以不用 ES5 shim 因为 IE11 支持其中的大部分内容吗?

是的,在那种情况下删除 ES5 shim 是安全的。我使网站可用 IE11,常见问题是自定义变量,显然是 ES6 和 CSS calc() 相关的错误。设法使用地图制作我自己的自定义变量模拟器,并使用了类似的方法 CSS calc()。

他们这里用的length属性,其实是来自于Function构造函数的原型,Function.prototype.length和returns期望的参数量由 function。正如您在我链接的页面上看到的那样,属性 是 不可枚举的 ,因此 for ... in 不应枚举给定的 属性。以下代码片段演示了 属性 不可枚举,因此 result 将保持 true

var result = true;
for(var propertyName in Function) {
  if(propertyName == 'length') {
    result = false;
  }
}

var isLengthEnumerable = Function.prototype.propertyIsEnumerable('length');
console.log('Function.prototype.length is enumerable: ' + isLengthEnumerable);
console.log('Result: ' + result);

上面的代码片段为我们提供了以下输出:

Function.prototype.length is enumerable: false
Result: true

但在 javascript 中,一切都是对象,并通过其原型链从 Object.prototype 继承属性,包括 Function。那么当我们将相同的 属性 length 分配给 Object.prototype 时会发生什么呢?

var result1 = true;
var result2 = true;
Object.prototype.length = 42;
Object.prototype.otherProperty = 42;
for (var propertyName in Function) {
    if (propertyName == 'length') {
        result1 = false;
    }
    if (propertyName == 'otherProperty') {
        result2 = false;
    }
}

var isLengthEnumerable = Object.prototype.propertyIsEnumerable('length');
var isOtherPropertyEnumerable = Object.prototype.propertyIsEnumerable('otherProperty');
console.log('Object.prototype.length is enumerable: ' + isLengthEnumerable);
console.log('Object.prototype.otherProperty is enumerable: ' + isOtherPropertyEnumerable);
console.log('Result1: ' + result1);
console.log('Result2: ' + result2);

上面的代码片段为我们提供了以下结果:

Object.prototype.length is enumerable: true
Object.prototype.otherProperty is enumerable: true
Result1: true
Result2: false

由于我们刚刚分配的 Object.prototype.length 属性 可枚举的,因此您会期望 result1 现在是 false .但是因为Function已经有一个length属性(虽然不可枚举),所以继承自Object.prototypelength属性不被枚举。 属性 已被 隐藏

这在 IE11 中不会发生,Object.prototype.length 无论如何都会被枚举,result1 也会变成 false