자바에서는 특정 코드 섹션에서 함수가 호출되지 않도록 할 수있는 방법이 있습니까? "alert"함수가 코드의 특정 섹션에서 호출되지 않았는지 확인하려고합니다.코드의 특정 부분에서 함수가 호출되지 않도록하기
이 경우alert("Hi!"); //this should work normally
var al = alert
//the function "alert" cannot be called after this point
preventFunctionFromBeingCalled(alert, "Do not use alert here: use the abbreviation 'al' instead.");
alert("Hi!"); //this should throw an error, because "console.log" should be used instead here
allowFunctionToBeCalled(alert);
//the function "alert" can be called after this point
alert("Hi!"); //this should work normally
, 어떻게 기능 allowFunctionToBeCalled
및 preventFunctionFromBeingCalled
을 구현해야합니까?
window._alert = window.alert;
window.alert = function() {throw new Error("Do not use alert here, use console.log instead");};
// later:
window.alert = window._alert;
delete window._alert;
을하지만 주요 hax입니다 :
전역이 아닌 범위에 있다면 로컬 변수 –
Nope를 사용하여 경고 마스킹으로 벗어날 수 있습니다. 실제로 누군가가 실제로'window.alert'를 지정하는 것은 드문 일입니다. –