What are pitfalls of extending Object.prototype?

前端 未结 2 1180
生来不讨喜
生来不讨喜 2020-12-11 22:21

I want to extend Object.prototype, to basically support notifications in JSON data and html elements through UI framework.

Object.prototype.setValue = functi         


        
2条回答
  •  难免孤独
    2020-12-11 22:35

    I know it fails for(var x in obj), but mostly we can use obj.hasOwnProperty, that should help right?

    That is the only major pitfall, and .hasOwnProperty() helps, but only in your own code. In jQuery, for example, they've taken the deliberate decision not to call .hasOwnProperty in the $.each() method.

    I have used Object.defineProperty(...) in my own code to get around the enumerable property problem with no ill-effects (albeit on Array.prototype)

提交回复
热议问题