I feel a little ignorant here, but I figure some education won't hurt me.
I have simplified my code to outline the issues - the problem is that when foo() is called from within the init method, it cannot access the supposed globals, a, b, c and settings. These are defined outside the method scope, so why are they not accessable?
For instance, settings is clearly defined just before the foo call, so why can foo not see settings?
(function(jQuery) {
a = new Date();
var b;
var c = 1;
settings = 0; // Here or not - same issue
var methods = {
init : function(settings) {
c = $(this);
settings = jQuery.extend({
id: Math.floor(Math.random()*1001),
x: 3;
etc: 2
}, settings || {});
m = 1;
foo();
}
};
function foo()
{
x = settings.x; // settings is not defined
var n = c.m;
return x;
}
jQuery.fn.bar= function(method) {
if (methods[method]) // If we have a method that exists
{
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else if ( typeof method === 'object' || ! method ) // Otherwise if we get passed an object (settings) or nothing, run the init.
{
return methods.init.apply( this, arguments );
}
else
{
$.error( 'Method ' + method + ' does not exist' ); // Otherwise we have an error.
}
};
})(jQuery);
Any ideas?