how do I override appendChild()?

后端 未结 2 752
暗喜
暗喜 2021-02-08 10:36
appendChild = function(message) {
    console.log(\"intercepted!\");
}

using the code above does not seem to work.

Anyone knows?

2条回答
  •  一整个雨季
    2021-02-08 11:14

    It is ill advised to overwrite native functions, but if you do it than make sure you return the appended element as well to prevent problems with code that uses the returned value of the native "appendChild" function:

    window.callbackFunc = function(elem, args) {
      // write some logic here
    }
    window.f = Element.prototype.appendChild;
    Element.prototype.appendChild = function() {
        window.callbackFunc.call(this, arguments);
        return window.f.apply(this, arguments);
    };
    

提交回复
热议问题