Skip to content Skip to sidebar Skip to footer

Is There A Way To Tell If A Function In Javascript Is Getting Over Written?

Is there a way to tell if a function I've written is getting overwritten or redefined? For example, say a guy named Jack has the function below: // Jack's function on team 1 functi

Solution 1:

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.

Post a Comment for "Is There A Way To Tell If A Function In Javascript Is Getting Over Written?"