javascript - Object in NodeList prototype -
the prototype of native javascript objects can extended include new functions (such nodelist.prototype.foreach or nodelist.prototype.addeventlistener) use allow array , element-like interaction nodelist. far good, how should add object prototype in turn has it's own functions (to allow nodelistvar.classlist.remove("class")). have been able make nodelistvar.classlist().remove("class"), doing following
nodelist.prototype.classlist = function(){ var _this = this; return { remove: function(class){ _this.foreach(function(){ this.classlist.remove(class); }); } } }; however prefer syntax same normal element, more like:
nodelist.prototype.classlist = { remove: function(class){ //where *this* nodelist , *not* domwindow this.foreach(function(){ this.classlist.remove(class); }); } }; it isn't hard even, have searched google endlessly , looked through countless questions , can't find usefull.
first read: what’s wrong extending dom.
you can't set objects on prototype. called functions executed in context of static prototype object, not nodelist itself. object on prototype has no reference current nodelist.
on normal elements, every element has own classlist attribute, domtokenlist bound element. need same: give every nodelist own classlists instance. can't in unavailable constructor, have use getter, demonstrated.
i don't think should try allow same syntax on nodelists on elements, because different. if want rid of these brackets, can install native getter function.
function classlists(nl) { ... // nl references nodelist we're bound } classlists.prototype = ...; object.defineproperty(nodelist.prototype, "classlists", { get: function() { return this.classlists = new classlists(this); } });
Comments
Post a Comment