-1

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)

Community
  • 1
  • 1
Aman Gupta
  • 5,548
  • 10
  • 52
  • 88

3 Answers3

2

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.

jasonscript
  • 6,039
  • 3
  • 28
  • 43
  • @jsonscript Sound interesting.. could you suggest some way out ? any idea ? – Aman Gupta Nov 12 '13 at 05:40
  • 1
    There's heaps. Look up the `Revealing Module Pattern` and `Immediately Invoked Function Expressions (IIFE)` and other coding patterns. Look at the way jQuery creates it's methods without exposing all it's variables... – jasonscript Nov 12 '13 at 05:51
0

in the context of a browser all global functions are attached to the window object so if you have

function myFunction() {
   alert("Hey!");
}

then

myFunction();

and

window.myFunction();

are the same calls

and yes you can define you function like window.myFunction = function() { /*...*/ } but it's just weird

Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • Yes, I agree. so basically what happen my contents are being generated through server using ejs templates and if I have some function onclick event of some element, and that function is present already into some other js already loaded, in that case I used `window.myFunction` (may be my implementation is little buggy, but I have faced this issue) btw thanks for you answer :) – Aman Gupta Nov 12 '13 at 04:46
0

In Javascript programming there is a global object which is a root object that holds all the global variables. The global object depends on the runtime which you are using. Not sure of Node.js but in case javascript running in a browser, the global object is window object. If you are creating a global variable, you are creating property on the window object.


    var def="Hello";

You can access above value using def variable or using window.def This works on functions too.


    function myFunc(){
       console.log("Hello");
    }

So, if you see window object you will find that myFunc property is created which is holding this function. You can execute this function using myFunc(); directly or using window.myFunc();

Global variables causes chaos, am not much in favour of global variables. Try using IIFE. Follow below link.

http://jkoder.com/avoid-polluting-global-namespace-in-javascript-iifeimmediately-invoked-function-expression/

Anoop Rai
  • 459
  • 5
  • 6