Can I subclass a DOM-class?

后端 未结 4 1862
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:30

    In browsers where __proto__ is exposed and mutable you can sub class DOM elements. It looks like this:

    function CustomEl () {
        var el = document.createElement('div')
        el.__proto__ = CustomEl.prototype
        return el
    }
    CustomEl.prototype.__proto__ = HTMLDivElement.prototype
    

    I also played with it in more detail on jsFiddle. Unfortunately though IE and Opera don't allow __proto__ access and have no plans to in the future as far as I know.

提交回复
热议问题