I agree with Amin Jafari that it is not worth replacing the built-in functions at all, but there may be times when it is useful for testing or for other reasons.
However, the reason your code does not work is because your alert() replace function is in this form:
function alert( text ) { ... }
Function declarations are processed before any other code in the same area is executed. This is sometimes called the "lift function", although this is a bit wrong. (The function does not actually move, as the term "rise" implies).
In any case, this replaces the built-in alert() function before you save it in your built_in_alert variable.
Do it like this:
alert = function( text ) { ... }
Since you use the usual assignment to replace the built-in alert() , the replacement happens when and where you expect it.
Try it here:
built_in_alert = alert; alert = function( text ) { console.log( 'Alert function called with param :'+ text ); built_in_alert( 'Calling with ' + text ); } alert( 'hi' );
source share