myUnchangeableFunction

myUnchangeableFunction

Although the suggestion on namespacing is apt, you may also be interested in the writable and configurable properties of Object.defineProperty descriptors if you wanted to prevent overwriting of specific functions:Object.defineProperty(window, "myUnchangeableFunction", {value: function () { alert("Can't change me");}, writable: false, configurable: false});/*function myUnchangeableFunction () { // TypeError: can't redefine non-configurable property 'myUnchangeableFunction' alert('change?');}var myUnchangeableFunction = function () { // TypeError: can't redefine non-configurable property 'myUnchangeableFunction' alert('change?');};*/myUnchangeableFunction();但是,由于某些较旧的浏览器可能不支持此功能,因此应在希望支持的浏览器中检查支持.You should check support in the browsers you wish to support, however, as some older browsers might not support this functionality. 这篇关于有没有办法判断JavaScript中的函数是否已被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 17:25