I was reading a article on javascript debugging session, where author needed a way to get into removeChild of Element for knowing, which code is removing a particular element at run time.
So he used following code for purpose
javascript:void(Element.prototype.removeChild=function(){undefined()})
As far as I know, “undefined is a property of the global object, i.e. it is a variable in global scope.”(Quoted from MDN) typeof which being undefined.
In the next line, author says that he gets the stack trace(Note that he is working with opera and dragonfly).
I have tried executing this code, and it works if used at it is, but If I try to use only
undefined()
It does give me error “TypeError: undefined is not a function”, which I understand.
Can anybody please explain how the mentioned code is working as valid javascript and how it solves problem of getting stack trace.
When the author calls
undefined(), it causes an error to occur, which he’s catching in the debugger so he can figure out who’s callingremoveChildon that element.Another approach would be to use the
debugger;statement instead, which forces a breakpoint if the debugger is open.