Is there any way to know if a function is written in JavaScript?

Is there a way to find out if the function I wrote is overwritten or redefined? For example, let's say a guy named Jack has a function below:

// Jack function on team 1 function doSomething() { // doing things } 

and one day he learns that a teammate at the north pole of the planet Zaksson used the same function name:

 // Zaxar function on team 2 function doSomething() { // doing something else } 

As you can see, Saxxar and Jack use the same function name. Neither Saxar nor John discovered the problem until 6 months in the project. They know that they can put a warning in their function and check it at runtime, but let Zaxar and Jack have 2 trillion functions, and they use 1 million JavaScript libraries, and they understand that it is not practical to place warnings in each function.

Is there a way to help Sachsar and Jack find out if there are any name conflicts at run time? Or is there a parameter that tells the JavaScript engine to cause name collision errors?

Additional Information
I load several libraries, and I think one of them uses the same function name as I do. So in this example, there really is no other developer with whom I can correlate. It discovers that the library (s) I am loading into uses the same function names and prevents something like this from happening along the line.

+3
source share
1 answer

Although the naming convention is limited, you might also be interested in the writable and configurable Object.defineProperty descriptor properties if you want to prevent overwriting of certain 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(); 

However, you should check the support in the browsers you want to support, as some older browsers may not support this functionality.

+1
source

Source: https://habr.com/ru/post/1260425/


All Articles