javascript - getting function itself among its parameters -
have simple function applying given attributes dom-element:
object.prototype.setattr = function(attr) { // attr object ( var in attr ) this.setattribute( i, atr[i] ); };
in addition passed parameters i'm getting function setattr itself. can explain behavior?
you need use hasownproperty avoid looping through prototype chain.
since attr
object , have added setattr
prototype of object
, attr has access setattr
method through prototype chain.
object.prototype.setattr = function(attr) { // attr object (var in attr) { if (attr.hasownproperty(i)) this.setattribute(i, attr[i]); } }; item.setattr({ b: 1 });
<pre id="result"></pre> <p id="item">item</p>
Comments
Post a Comment