覆盖HTMLNode接口 [英] Overwrite the HTMLNode interface

查看:27
本文介绍了覆盖HTMLNode接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入 Node 构造函数.可以用我自己的Node构造函数覆盖公共Node构造函数吗?

I would like to hook into the Node constructor function. Is it possible to overwrite the public Node constructor with my own Node constructor?

当前仅在chrome/firefox中测试

Currently only testing in chrome/firefox

(function() {
    var _Node = window.Node;

    var Node = function() {
        Event.trigger("nodeCreation", this, arguments); // pseudocode
        _Node.apply(this, arguments);    
    }

    window.Node = Node;

    console.log(document.createElement("div") instanceof _Node); // true
    console.log(document.createElement("div") instanceof Node); // false
}());

我很清楚如果我弄错了这会有多危险.我也知道,如果我能正确解决的话,它的功能非常强大.

I'm perfectly aware how dangerous this can be if I get it wrong. I'm also aware that this is powerfully versatile if I can get it right.

是否还有其他方法可以覆盖本机DOM对象.扩展其原型对于覆盖构造函数没有用处

Is there any other way to overwrite native DOM objects. Extending their prototype isn't a useful as overwriting the constructor

推荐答案

不是.您不能为DOM Node或任何其他DOM对象重新定义构造函数(但是可以为JavaScript对象重新定义).

Nope. You cannot redefine constructor either for DOM Node or for any other DOM object (you can however do that for JavaScript objects).

此外,尝试重新定义DOM对象构造函数绝对没有用,因为它们通常不能直接调用(Image和其他几个除外),因此参数问题是无关紧要的.可以使用Mutation Events跟踪DOM的变化.

Also, it is absolutely useless to try and redefine DOM objects constructors, since they generally cannot be invoked directly (except for Image and a couple of others), thus the issue of arguments is irrelevant. Tracking the DOM changes can be done using Mutation Events.

这篇关于覆盖HTMLNode接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆