【问题标题】:Is ES5 shim still needed for Internet Explorer 11?Internet Explorer 11 是否仍需要 ES5 垫片?
【发布时间】:2018-11-24 15:51:39
【问题描述】:

我正在查看compat tables,似乎 IE 11 支持所有 ES5,除了(在 Miscellaneous 下)此功能:“可枚举的属性可以被不可枚举的对象遮蔽”。尽管提供了示例,但我不明白这意味着什么。

由于 IE11 支持大部分 ES5 shim,我可以放弃使用它吗?

【问题讨论】:

    标签: javascript ecmascript-5 es5-shim


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      他们在这里使用的length 属性实际上来自Function 构造函数的原型Function.prototype.length,并返回function 期望的参数数量。正如您在我链接的页面上看到的那样,该属性不是可枚举的,因此for ... in 不应枚举给定的属性。以下 sn-p 表明该属性不可枚举,因此 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);

      上面的 sn-p 给了我们以下输出:

      Function.prototype.length 是可枚举的:false
      结果:真

      但在 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);

      上面的sn-p给我们的结果如下:

      Object.prototype.length 是可枚举的:true
      Object.prototype.otherProperty 是可枚举的:true
      结果1:真
      结果2:假

      由于我们刚刚分配的Object.prototype.length 属性 可枚举的,您会认为result1 现在将是false。但是因为Function 已经有一个length 属性(虽然不能枚举),所以从Object.prototype 继承的length 属性没有被枚举。该属性已被遮蔽

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 2018-03-27
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多