Overriding native function?

后端 未结 7 1076
半阙折子戏
半阙折子戏 2020-12-30 05:00

The native document.createElement() is silly-stupid (it takes only a tag name and no attributes). How come I can\'t override it? How come this doesn\'t work?

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 05:22

    FWIW (informational): you can override "native" methods, in some cases, and in some browsers at least. Firefox lets me do this:

    document.createElement = function(f) { alert(f); };
    

    Which then does as you expect when invoked. But your whole block of code above throws an error, at least via Firebug.

    Philosophically, you should be able to do this. You can certainly, say, redefine methods on the Array object, etc. But the window (DOM) methods are not covered by ECMAScript, and so they're probably allowed to be implementation-dependent. And of course, they are this way for security reasons.

提交回复
热议问题