javascript-如何扩展本机DocumentFragment类?

javascript-如何扩展本机DocumentFragment类?,javascript,dom,Javascript,Dom,我想通过HtmleElement#appendChild将我的DomBuilder附加到html中。 并替换为jQuery#html 我试过这个 var Fragment = function() { }; Fragment.prototype = Object.create(DocumentFragment.prototype); Fragment.prototype.append = function(el) { if (el instanceof DomBuilder) {

我想通过HtmleElement#appendChild将我的DomBuilder附加到html中。 并替换为jQuery#html


我试过这个

var Fragment = function() {
};


Fragment.prototype = Object.create(DocumentFragment.prototype);
Fragment.prototype.append = function(el) {
    if (el instanceof DomBuilder) {
        return this.append(el.build());
    }

    if (el instanceof Element) {
        this.appendChild.(el);
    }

    return this;
};

$dom.frag = function() {
    var f = new Fragment();

    return f;
};
但是

抛出“未捕获的TypeError:非法调用”。 我需要超级对象(docuemtnframent)来完成此操作



第二次尝试:

$dom.frag = function() {
    var f = document.createDocumentFragment();
    f.append = function(el) {
        if (el instanceof DomBuilder) {
            return this.append(el.build());
        }

        if (el instanceof Element) {
            this.appendChild(el);
        }

        return this;
    };
    return f;
};
工作正常,但这将为每个$dom.frag()调用创建函数“append”


第三次尝试:

DocumentFragment.prototype.append = function(el) {
    if (el instanceof DomBuilder) {
        return this.append(el.build());
    }

    if (el instanceof Element) {
        this.appendChild(el);
    }

    return this;
};


$dom.frag = function() {
    var f = document.createDocumentFragment();
    return f;
};
这可以正常工作,并共享函数“append”。 但是这样行吗?没有跨浏览器问题


此网站将支持ie9~/其他现代浏览器

您到底想做什么?jQuery在内部使用片段,因此无需重新发明轮子。如果不打算使用jQuery,只需使用appendChild添加到片段?@adeneo它是我的dom Manipulation库。如果站点需要响应,并且必须支持android,我更喜欢使用纯javascript。那么最后一个可以,它将append方法原型化到所有片段中。@adeneo谢谢。我现在可以节省谷歌的时间了。
DocumentFragment.prototype.append = function(el) {
    if (el instanceof DomBuilder) {
        return this.append(el.build());
    }

    if (el instanceof Element) {
        this.appendChild(el);
    }

    return this;
};


$dom.frag = function() {
    var f = document.createDocumentFragment();
    return f;
};