Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

I need to define a Global variable ?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know there are 3 approaches for making a global variable..

But i don't know which is the best 1 ?

Method 1: Explicitly declare it outside of any function scope:




Method 2: Implicitly use a global variable by not using 'var' inside the function scope:




Method 3: Define a variable in the window scope:




I actually liked the 3rd approach, just simply attaching to the window object. I am right here ?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The third way is a lot of typing and adds to the "weight" of the page.

Must JavaScript developers try to avoid global variables at all costs and you really do not need them if you use OOP style of programming with JavaScript.

Eric

 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric for your reply.

But I know that declaring a variable outside the function, make's it a global variable.
The global variable itself get's attached to window.

If i am wrong, please advise me, which approach should i use out of 1, 2 and 3.

I know global variable's are costly and lack's security. but in my case i need it.

I have 1 parent window and has 2 child window's. So i want to access a variable from one of child window and use it in another child window.

By assiging a global variable in parent window from 1st child window. I will be able to use global variable in other 2nd child window.

Do you have any other suggestion's ?

 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When global variables are necessary which option do you think is most clear?

They all do essentially the same thing, why would you choose formats that make the code hard to understand?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me, the 1st option is clear.

But for better technical understanding, look's like the 3rd option is good.

But can you let me know, how to implement my intended functionality without the use of global variable's ?
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:But for better technical understanding, look's like the 3rd option is good.

How so?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you use the first option, anybody can understand, that the global variable is declared.

But in reality, behind the scene's it is actually being attached to the window object.

As for 3rd approach, for better technical understanding, it is being shown that whatever is happening behind the scene's.
We are showing it is being attached to the window object.

Hope it clears the point.

but my main doubt is being lieing pooled ?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please advise !
 
Bear Bibeault
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic