I want to override one Element.prototype function:
const originalAppendChild = Element.prototype.appendChild;
Element.prototype.appendChild = (el) => {
console.log("Override!!!");
return originalAppendChild.apply(this, [el]);
};
But when I then run:
document.body.appendChild(document.createElement("div"));
I get:
Override!!!
VM406:5 Uncaught TypeError: Illegal invocation
at HTMLBodyElement.Element.appendChild (<anonymous>:5:36)
at <anonymous>:1:15
Where line 5:36 refers to the .apply(this, [el]);
part. Which means it this the apply
method who is throwing this error.
I have tried to figure out, and it seems the problem comes from this
, but I am at loss how to properly set it (if that's the cause).