一部のネイティブ JavaScript オブジェクトのプロトタイプを拡張して、新しい関数 (NodeList.prototype.forEach
または などNodeList.prototype.addEventListener
) を含めることができます。これを使用して、NodeList
. これまでのところは良いですが、独自の機能を持つプロトタイプにオブジェクトを追加するにはどうすればよいですか (許可するためNodeListVar.classList.remove("class")
)。私はNodeListVar.classList().remove("class")
次のようにして、作ることができました
NodeList.prototype.classList = function(){
var _this = this;
return {
remove: function(class){
_this.forEach(function(){
this.classList.remove(class);
});
}
}
};
ただし、構文が通常と同じであることを強く希望しますelement
。したがって、次のようになります。
NodeList.prototype.classList = {
remove: function(class){
//where *this* would be the nodeList and *not* the DOMWindow
this.forEach(function(){
this.classList.remove(class);
});
}
};
それはおそらく難しいことではありませんが、私はグーグルを際限なく検索し、数え切れないほどの質問に目を通しましたが、有用なものは何も見つかりません.