Can I subclass a DOM-class?

后端 未结 4 1863
Happy的楠姐
Happy的楠姐 2021-01-18 02:23

I was wondering if I can create a subclass of HTMLDivElement. Like this.

MyDivElement.prototype.pickColor = function()
{
    return this.picked;
}
function M         


        
4条回答
  •  温柔的废话
    2021-01-18 03:18

    In some browsers, you can extend the prototype, in others, no. I'll let you guess the ones where you can't. :-) That's not really the same as extending a DOM element, but it does let you do a certain subset of the things for which you might want that facility. The thing is, DOM elements aren't really JavaScript entities; they're only simulacrums provided by the runtime system. (Maybe someday all the jsdom work will actually come to fruition.)

    Well ok I'll tell you about the problematic browsers: IE doesn't like that at all. However others do. If you've ever looked at the Prototype library, you'll come across a manifestation of that fact all the time via nasty irritating IE-only bugs when you forget to Prototype-ize a DOM element reference.

    (IE9 may be different, but I sort-of doubt it.)

    This is the kind of thing that's dirt simple to test over at jsfiddle.

提交回复
热议问题