Jump to content

alternative to using globals


jimfog
 Share

Recommended Posts

Globals are bad...we know that.

 

But if I want to make available to a function the variable from another function what can I do.

 

Is the answer to use this variable as an argument?

 

Is there any other way?

Link to comment
Share on other sites

You're confused. Globals aren't bad, never were. Its the act of abusing Globals (and how easy people can do so) that is bad. Globals is a necessary part in nearly all languages. That said, if you can get away with avoiding the Global scope then its often for the best. Using Globals when other options are exhausted is completely fine, you just need to keep track of them better.

 

Anyway, your question can solved either with scoping or via the use of accessor methods.

 

Scoping: You can wrap both functions and the variable into one object, all defined in the same scope your functions and variables are all grouped together and only the "myobject" touches the global scope:

var myobject = {myvariable:0,One:function (){this.myvariable+=1;console.log(this.myvariable);},Two:function (){this.myvariable+=2;console.log(this.myvariable);},};//outputs "1" in consolemyobject.One();//outputs "3" in consolemyobject.Two();

accessor: You can have object 'A' with the variable 'count', function 'One', and has a getter method 'getCount' while the other function (which belongs in object 'B') can simply use A.getCount() to get the count.

var A = {count:0,One:function(){count++;},getCount:function(){return this.count}setCount:function(x){this.count = x}}var B = {Two: function(){ var c = A.getCount()+2; A.setCount(c);}}

There is a more stable way of writing getters and setters, but its a bit more verbose than I need to be for this simple example. you can read up on MDN about defineProperty and defineProperties

 

 

 

Which option you should best use (Scoping vs. getters) depends on how the two functions relate to each other.

Edited by Hadien
Link to comment
Share on other sites

Well the problem in my case is that the code is found in a plugin(I mean is not my code) and I am trying to find a way to mess things as little as possible.

Link to comment
Share on other sites

I'm not sure I follow. jqeury is merely a framework, you could use the examples I made above alongside them. unless you mean specific objects created inside jquery well you can extend those objects with $.(origObject, extendingObject). or, for example with jQueryUI, use Widget Factory (view source). Writting accessor methods for them.

 

I've written a UI Widget example before on the site a while back for a specific user. I didn't write any accessor methods per se in that example, but I could have easily done something like:

getCurrent:function(){  return this.currentTooltip},
Link to comment
Share on other sites

Anyway...this is the plugin http://arshaw.com/fullcalendar/ and this is the file where modifications must be done https://onedrive.live.com/?cid=BE27434B2AAC8130&id=BE27434B2AAC8130%21321

 

It has nothing to do with jquery.

Link to comment
Share on other sites

Maybe...

if (typeof jfog === 'undefined'){var jfog = {};//project namespace}else{alert('Namespace conflict');throw {name:'error',message:'Namespace conflict'};}
Link to comment
Share on other sites

there are a number of patterns to address the topic of dealing with globals. namespace, module, and sandbox patterns come to mind.

https://www.google.com/search?q=namespace+pattern&oq=namespace+pattern&aqs=chrome..69i57j0l5.3149j0j7&sourceid=chrome&es_sm=119&ie=UTF-8

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...