Just chiming in here.
First, not declaring variables is a very bad practice. you are essentially
exploiting a poor design in JavaScript in a way that can have negative
consequences (i.e. destroying a variable that may already exist).

You should always declare variables with var. Always.

Secondly, scope in JavaScript is only created by functions. This is the case
with all functions and all scopes, with the exception of the global scope,
which is created by the DOM (and is, again, a poor design aspect of the
language).

Using var to declare your variables does not prevent you from sharing access
to that variable. Consider this code:

window.addEvent('domready', function(){
var foo;
var alertFoo = function(){
alert(foo);
};
var setFoo = function(value){
foo = value;
};
setFoo('foo!');
alertFoo();
});


In this example, we have a variable defined outside two functions (also
defined by var, which you should do, as it both prevents global variables
and allows a function to reference itself). Both functions have access to
that variable.

If we took the domready statement away, we would still want the exact same
code. Regardless of the scope, this allows us to declare variables without
assuming anything.

Finally, let's say we did take away the domready statement. It would still
be in our best interests to wrap the entire statement in a function to
prevent global variables:

(function(){
var foo;
var alertFoo = function(){
alert(foo);
};
var setFoo = function(value){
foo = value;
};
setFoo('foo!');
alertFoo();
})();

The only global variables we should be creating in our application code
(ideally - this is not always practical) is a namespace for our application
and, perhaps, classes (MooTools treats all its classes as global objects -
Fx is Fx, not MooTools.Fx).

For example:
//global variables
var mySite = {};
var Widget = new Class(...);
//namespaced variables
mySite.foo = 'fooooo!';
mySite.widgetInstance = new Widget();
//code with private methods and variables
(function(){
var alertFoo = function(){
alert(mySite.foo);
};
var setFoo = function(value){
mySite.foo = value;
};
setFoo('foo!');
alertFoo();
})();

I probably wouldn't use the last part (the function encapsulation for the
alertFoo and setFoo methods), instead opting to make this a method in my
mySite object namespace, but there are times when this pattern is useful.


On Wed, Dec 17, 2008 at 7:52 AM, Michal-2 (via Nabble) <
[email protected]<ml-user%[email protected]>
> wrote:

>
> @Steve
>
> I completely agree with everything you have written in your previous
> post. My point, and solution to the original question, was about
> having "var" when declaring a variable in the *global* scope (*not* in
> any function). I didn't mean to suggest to "var" any variable within
> an addEvent function.
>
> (I was replying to your comment "no i wouldnt do that" when I
> suggested declaring the variable with "var" in the global scope)
>
> But did you mean that you shouldn't have global variables, as a point
> of programming standards? With this point I would also agree, I would
> try to minimise the number of global variables, say by wrapping things
> up in objects.
>
> Michal.
>
> On Dec 17, 3:36 pm, "Steve Onnis" 
> <st...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=0>>
> wrote:
> > Thats because at that point the variable "test" belongs to the document
> and
> > because its set on the document level anything can have access to it.
> >
> > Where as take this for example...
> >
> > <script>
> >
> >         function fooOne () {
> >                 var fooOneVar = "hello from fooOne";
> >                 }
> >
> >         function fooTwo () {
> >                 alert(fooOneVar);
> >                 }
> >
> >         fooTow();
> >
> > </script>
> >
> > This will produce an error because "fooOneVar" becomes a static value
> > "inside" the fooOne() function.
> >
> > In essence i am pretty sure this will also be the case if you were to var
> a
> > variable inside the addEvent function because all you are doing is adding
>
> > functions to the domReady property.
> >
> > Steve
> >
> > -----Original Message-----
> > From: 
> > mootools-us...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=1>
> >
> > [mailto:mootools-us...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=2>]
> On Behalf Of Michal
> > Sent: Thursday, 18 December 2008 2:21 AM
> > To: MooTools Users
> > Subject: Re: IE(6) and variables in domready
> >
> > @Steve:
> >
> > I have also just done a small test:
> >
> > http://paste.mootools.net/f49801ffb
> >
> > and the code alerts a value of "3" from the "var test" variable, so
> > I'm not sure if your point was right.
> >
> > Michal.
> >
> > On Dec 17, 3:15 pm, Michal 
> > <michalchare...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=3>>
> wrote:
> > > @Steve:
> >
> > > I agree that there shouldn't be a "var mainMenu" in a function scope
> > > if it is to be used elsewhere. However, I've had a quick search, and
> > > from what I've found if you "var" something in global scope it becomes
> > > global (and thus available in all other scopes, such as in a domready
> > > function). Perhaps I am still confused...
> >
> > > Michal.
> >
> > > On Dec 17, 2:29 pm, stratboy 
> > > <em...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=4>>
> wrote:
> >
> > > > Yes Michael, that's a solution I think. I was just trying a similar
> > > > thing adding mainMenu as a window property and it worked:
> >
> > > > window.addEvent('domready', function() {
> > > >         window.mainMenu = new Menu({ menuId:'menu', type:'bar',
> > > > props:mainMenu });
> >
> > > > });
> >
> > > > anyway, probably your suggestion is more efficient, couse that way I
> > > > don't touch the window object :)
> >
> > > > Anyway, the thing that still remains quite strange to me, is that the
>
> > > > first version I posted worked right in Safari and FF, and the problem
>
> > > > is only with IE. Mmm... Maybe it's not a thing  so strange..
> >
> > > > On 17 Dic, 15:22, Michal 
> > > > <michalchare...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=5>>
> wrote:
> >
> > > > > Perhaps I've got the var thing a bit confused, but I would put
> >
> > > > > var mainMenu;
> >
> > > > > in the global scope, and then all later references to mainMenu
> should
> > > > > refer to that.
> >
> > > > > Michal.
> >
> > > > > On Dec 17, 2:12 pm, "Steve Onnis" 
> > > > > <st...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=6>>
> wrote:
> >
> > > > > > silly question....
> >
> > > > > > which file is being included first?
> >
> > > > > > -----Original Message-----
> > > > > > From: 
> > > > > > mootools-us...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=7>
> >
> > > > > > [mailto:mootools-us...@...<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1668458&i=8>]
> On Behalf Of stratboy
> > > > > > Sent: Thursday, 18 December 2008 1:11 AM
> > > > > > To: MooTools Users
> > > > > > Subject: IE(6) and variables in domready
> >
> > > > > > Hi! I've got a little problem with domready in ie6:
> >
> > > > > > window.addEvent('domready', function() {
> > > > > >         //no var key, so mainMenu should be global
> > > > > >         mainMenu = new Menu({ menuId:'menu', type:'bar',
> > > > > > props:mainMenu });
> > > > > > });
> >
> > > > > > later, in another .js, file
> >
> > > > > > window.addEvent('domready', function() {
> > > > > >         mainMenu.setStartButton({ mainIndex:6, subIndex:6,
> > > > > > openSub:true });
> > > > > > });
> >
> > > > > > Safari and Firefox  are just happy, but IE6 (don't know IE7) says
>
> > that
> > > > > > mainMenu is not defined.
> >
> > > > > > This seems to be a mootools 1.2.1 issue, since in another site
> using
> > > > > > 1.2.0, the same classes with the same methods work fine in IE6.
> >
> > > > > > Any idea? Is that maybe the two domready don't execute in
> sequence?
> >
> > > > > > If can be of help, I can post links to pages.
> >
> > > > > > Bye!
>
>
> ------------------------------
>  View message @
> http://n2.nabble.com/IE%286%29-and-variables-in-domready-tp1668032p1668458.html
> To start a new topic under MooTools Users, email
> [email protected]<ml-node%[email protected]>
> To unsubscribe from MooTools Users, click here< (link removed) >.
>
>
>


-----
The MooTools Tutorial:  http://www.mootorial.com www.mootorial.com 
Clientcide:  http://www.clientcide.com www.clientcide.com 
-- 
View this message in context: 
http://n2.nabble.com/IE%286%29-and-variables-in-domready-tp1668032p1668805.html
Sent from the MooTools Users mailing list archive at Nabble.com.

Reply via email to