Lots of discussion here since my last post, but I'll try and clear a few
things up:
Why you should always use var, even for global variables:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The same reason you should end every declaration with a semi-colon: for the
times when it matters. If you aren't in the habit of declaring your
variables with var, you'll blow away some global variable without meaning
to. Consider this statement:
window.addEvent('domready', function(){
nav = $('nav');
nav.tween.... or whatever
});
Who knows if there's a variable in the global scope that uses nav? In
theory, you should, but you might have some older code somewhere that looks
just like this or maybe an ad that is running with some poorly written
javascript. Using var ALWAYS protects your code, so always us it.
I'll reiterate that you should avoid global variables anyway, but of course
it's not always practical.
Closures and scope
~~~~~~~~~~~~~~~
>Basically when you var a variable it becomes scoped and is only accessible
>from the function you have set it in.
This is true, but the nuance is that it's available to any inner functions
in that scope for as long as they live. Consider this code:
window.addEvent('domready', function(){
var welcome = $('welcome');
var fadeIn = function(){
welcome.reveal();
};
if(someConditional) { fadeIn(); }
else { fadeIn.delay(1000); }
});
The variable we defined - welcome - is declared, then our method - fadeIn -
is declared, and then we have some conditional that determines if we're
showing the welcome message now or in a second. The delay calls fadeIn a
second after the domready statement concludes, but the fadeIn method
continues to have access to welcome.
Scope is unidirectional; things go down but not up so long as you use var
with your declarations. When your code gets more and more rich, it becomes
even more important to know where your variables entered scope.
BTW, the code example above is kind of ridiculous; just using it to
illustrate the concept. You could express that logic in far less code, of
course.
mainMenu is null
~~~~~~~~~~~~
The code you were using is this:
var mainMenu = null;
window.addEvent('domready', function() {
mainMenu = new Menu({ menuId:'menu', type:'bar',
props:mainMenu });
});
First, I'll point out that you don't need the "null" in there. You can just
do "var mainMenu;" - same thing.
Anyway, I suspect that you were getting that error - that mainMenu is null -
because you are including it as an argument:
var mainMenu = null;
window.addEvent('domready', function() {
mainMenu = new Menu({
menuId:'menu',
type:'bar',
props:mainMenu //HERE
});
});
You are passing a null value to your constructor. Think about it. You are
defining mainMenu as the product of calling new Menu, but if the initialize
method references that option value before Menu returns, it's still null.
That might not be it, but it's still problematic. You can't do this:
var makeFoo(aFoo) {
return aFoo + ' is a Foo';
};
var foo = makeFoo(foo);
If you need to access an instance of a class, us the 'this' keyword.
On Thu, Dec 18, 2008 at 6:13 AM, Michal-2 (via Nabble) <
[email protected]<ml-user%[email protected]>
> wrote:
>
> > @Steve:
> > Sorry if this is slightly annoying now, but I'm still unclear by why
> > you wouldn't do what I suggested in my original post in this topic...?
> Sorry, I now realise that there shouldn't be a difference in:
>
> var mainMenu (in global) and
> mainMenu (in a local scope)
>
> So my suggestion didn't make much sense (apart from the fact that it
> seemed to work... lol)
>
> Michal.
>
>
> ------------------------------
> View message @
> http://n2.nabble.com/IE%286%29-and-variables-in-domready-tp1668032p1673104.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-tp1668032p1673830.html
Sent from the MooTools Users mailing list archive at Nabble.com.