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

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -