Object.prototype in JavaScript

后端 未结 2 622
遇见更好的自我
遇见更好的自我 2021-01-18 11:21

I have some JavaScript code that defines a function getElementsByAttribute as follows:

Object.prototype.getElementsByAttribute = function(attr)          


        
相关标签:
2条回答
  • 2021-01-18 11:52

    Adding things to Object.prototype is a really bad idea. It will be added to every object, and that will cause unintended behavior, I guarantee it.

    Simply define your function and decorate it onto whatever objects you need dynamically.

    0 讨论(0)
  • 2021-01-18 11:59

    IE DOM elements aren't normal Javascript objects and do not inherit prototypes as you would expect.

    http://perfectionkills.com/whats-wrong-with-extending-the-dom/

    0 讨论(0)
提交回复
热议问题