简体   繁体   English

ChildNode.remove()polyfill与babel-polyfill

[英]ChildNode.remove() polyfill with babel-polyfill

I am using ChildNode.remove() and I described by Mozilla I need a polyfill for IE. 我正在使用ChildNode.remove() ,我在Mozilla中描述我需​​要一个用于IE的polyfill I am using webpack with the babel-polyfill configured: 我正在使用配置了babel-polyfill的webpack:

 "babel-polyfill": "^6.13.0",
 "webpack": "^2.4.1",

webpack.config.babel.js: webpack.config.babel.js:

    entry: ['babel-polyfill', join(__dirname, path, "index.web.js") ],

My assumption was that babel-polyfill would provide me all the common polyfill I needed - but it is not, I have an error in Internet Explorer 11 . 我的假设是, babel-polyfill会为我提供我需要的所有常见的polyfill - 但事实并非如此,我在Internet Explorer 11中有错误。 Is there another config I missed? 我错过了另一个配置吗?

Thank you 谢谢

The babel-polyfill packages just polyfills javascript objects as far as I know, Childnode.remove() is part of the DOM so babel won't do anything with it. 据我所知, babel-polyfill包只是polyfills javascript对象, Childnode.remove()是DOM的一部分,因此babel不会对它做任何事情。 I would suggest that you just use the polyfill suggested in the Mozilla documentation . 我建议你只使用Mozilla文档中建议的polyfill。

// from:https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

As an alternative to (or in addition to) the babel-polyfill, you can look at Polyfill.io . 作为babel-polyfill的替代(或补充),您可以查看Polyfill.io

Like babel-polyfill , Polyfill.io will provide core Javascript functionality (eg Array.from ), but unlike babel-polyfill, it also polyfills DOM behavior, (eg ChildNode.remove() ). babel-polyfill Array.from一样,Polyfill.io将提供核心Javascript功能(例如Array.from ),但与babel- Array.from不同,它还可以ChildNode.remove() DOM行为(例如ChildNode.remove() )。 By default, it uses the browser user-agent string to determine which polyfills are necessary, preventing modern browsers from needing to download polyfills they don't need. 默认情况下,它使用浏览器用户代理字符串来确定哪些polyfill是必需的,从而防止现代浏览器需要下载他们不需要的polyfill。

The main thing that Polyfill.io doesn't provide, which babel-polyfill does, is support for generator functions (provided by regenerator-runtime), so for full functionality, you'd want to include that instead of the whole babel-polyfill . 这个babel-polyfill没有提供的主要功能是支持生成器函数(由regenerator-runtime提供),因此对于完整功能,你需要包含它而不是整个babel-polyfill

I created a tiny polyfill npm. 我创建了一个小的polyfill npm。 Should make you life easier. 应该让你的生活更轻松。 https://www.npmjs.com/package/element-remove https://www.npmjs.com/package/element-remove

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

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