次の点を考慮してください。
console.log('parentNode' in Element.prototype
? Element.prototype.parentNode
: 'no parentNode here');
かなり無実ですよね?no parentNode
Chrome (およびおそらく Safari でも) で文字列が得られますが、Firefox (21) と IE (10) の両方で失敗します。
- ファイアフォックス:
value does not implement interface Node
- IE10:
invalid calling object
何故ですか?ほら、ルールのないゾーンとも呼ばれるホストオブジェクトゾーンに入りました。ポイントは、一部のプロパティは にアタッチされているように見えElement.prototype
ますが、そこからはアクセスできず、Element
インスタンスからのみアクセスできるということです。
それについて何ができるでしょうか?明らかな回避策は、違反者を「try-catch」ブロックでラップすることです。
var a, b = Element.prototype, arr = [];
for (a in b) {
try {
b[a];
arr.push(a);
} catch (e) {}
}