ChildNode#remove の Polyfill 作成経緯
ChildNode#remove
は親ノードを指定不要なので便利ですが、IE11- で Polyfill を適用しないと使えないという欠点があります。
そこで Polyfill を書くことを試みました。
定義する場所としては ChildNode.prototype.remove
を指定するのが妥当と思われますが、未対応の IE11- が ChildNode
の DOM Interface Object を持っているわけがありません。
そこで Element.prototype.remove
を定義する事にしました。
JavaScript
1Element.prototype.remove = function remove () { 2 if (this.parentNode) { 3 this.parentNode.removeChild(this); 4 } 5};
次に Google Chrome 49.0.2623.112 m, Firefox 45.0.1 で ChildNode#remove
が定義されている場所を確認したところ、次の結果になりました。
JavaScript
1Element.prototype.remove; // function remove () 2Object.getOwnPropertyDescriptor(Element.prototype, 'remove'); // {writable: true, enumerable: true, configurable: true} 3document.createElement('p').remove === Element.prototype.remove; // true 4ChildNode; // ReferenceError: ChildNode is not defined
ChildNode
は存在せず、Element.prototype.remove
が定義されているようです。
今後も Element.prototype.remove
に存在するか不明なので 'remove' in document.createElement('p')
で存在判定する事にしました。
JavaScript
1if (typeof Element === 'function' && !('remove' in document.createElement('p'))) { // IE11- 用の Polyfill 2 Element.prototype.remove = function remove () { 3 if (this.parentNode) { 4 this.parentNode.removeChild(this); 5 } 6 }; 7}
ChildNode#remove の定義場所はどこが正しい?
MDNにある Polyfill コードでは Element.prototype.remove
が定義されています。
DOM Standard の仕様書側では Interface ChildNode で定義されており、Element との関連性がわかりませんでした。
実際のところ、ChildNode#remove
はどこに定義するのが正しいのでしょうか。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/11 03:44 編集
2016/04/11 03:56
2016/04/11 15:23