On 14/03/07, Tom Holder <[EMAIL PROTECTED]> wrote:
> Cheers Sam,
>
> That doesn't make a lot of sense to my I have to be honest.
>
> 1. Why are you starting with (function($)
This means that it would execute immediately and any variables
contained within the function won't be seen by other scripts.

> 2. Why $.fn inside this block? why not jQuery.fn
Results in a saving of code. It is whatever you used in 1.

> 3. What's (jQuery) on the end of the function?
jQuery is the jQuery object and is passed on as the parameter (the $
at the start).

A (hopefully) simpler example:

(function(parameter1, parameter2) {
        var space = " ";
        alert(parameter1 + space + parameter2); // alerts 'hello world'
})("hello", "world");
alert(space); // space is not defined

It is one of the suggestions for authoring plugins:
http://docs.jquery.com/Plugins/Authoring#Custom_Alias_in_plugin_code

>
> Sorry... OO Javascript is a whole new world of Pain for me! :)
>
> Thanks
> Tom
>
>
> On 14/03/07, Sam Collett < [EMAIL PROTECTED]> wrote:
> >
> > On 14/03/07, Tom Holder < [EMAIL PROTECTED]> wrote:
> > > Bit of a simple question this I'm sure but how do I scope a variable to
> a
> > > plugin I'm creating and not expose it beyond my plugin? I do want the
> > > variable to be accessible to all the methods in my plugin though.
> > >
> > >  Thanks
> > > Tom
> >
> > You could write your plugin like this:
> >
> > (function($) {
> >     var myvar = "Element number: ";
> >     $.fn.myplugin = function() {
> >         return this.each( function(i) {
> >             alert(myvar + i);
> >         });
> >     }
> > })(jQuery);
> > alert(myvar); // undefined
> >
> > _______________________________________________
> > jQuery mailing list
> > discuss@jquery.com
> > http://jquery.com/discuss/
> >
>
>
>
> --
> Tom Holder
>
> Technical Director
> SimpleWeb Limited
> Great websites. Low cost. No catch.
>
> http://www.simpleweb-online.co.uk/
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>
>

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to