agpt agpt - 3 months ago 7
Javascript Question

Javascript: efficiency of window object for calling user defined functions

In my nodejs application, I am communicating or say calling functions present in two different js file using

window.myfunction = function(){...}
way. Indeed its working perfectly. I went through this question as well which helped me to learn a bit better.

My question/doubt is, that is it OK to use
window
object to call user defined functions ?

(OK in terms of performance at client side, cross browser compatibility and but obvious security.) Thanks.

(I am a sort of beginner in javascript programming)

Answer

This is what we're talking about when we talk about Global Scope

It's important to try and reduce the amount of code that we put in the Global Scope because it increases the chances that there will be a conflict with another library or file.

For example if you have companyA.js and demoB.js and they both contain a method called retrieveItems in the Global scope then there is going to be a conflict.

You, as the JS developer, need to be aware of this issue and try to take some steps to avoid these problems.