Just to give a little insight, Howard had said that his way was
'dirty'. He was using $('div ul').myNamespace(). myMethod(). This
isn't very dirty if you think about it, though. myNamespace() is a
constructor just like jQuery(). In order to have access to 'this', you
must be in object context. You cannot use 'this' if calling statically
which jQuery('div').myNamespace. myMethod() would imply. If you don't
need to access an object set, then you wouldn't necessarily need to
do: jQuery('div').myNamespace().someMethod().

Efficiency would be a problem if you were instantiating multiple
times, but it would have to be done A LOT to be noticeable:
jQuery('div').myPlugin().myMethod1();
jQuery('div').myPlugin().myMethod1();
etc....

But systematically, it's not any different than the following, which
also causes overhead:
jQuery('div').show();
jQuery('div').hide();

To make this more efficient:
var myPlug = jQuery('div').myPlugin();
myPlug.myMethod1();
myPlug.myMethod2();
OR, if chaining:
myPlug.myMethod1().myMethod2();
OR, if you don't need to use jQuery('div') and myPlugin() together
again:
jQuery('div').myPlugin().myMethod1().myMethod2();

A similar approach with jQuery() would be:
var div = jQuery('div');
div.show();
div.hide();
OR, if chaining:
div.show().hide();

All in all, jQuery('div').dialog('open') is faaaaaar dirtier than
jQuery('div').dialog().open() - which I don't believe is dirty at all
- and allows for a much more extensible and flexible API. You can even
extend the plugin if you wanted to for example:

$.fn.myNamespace.fn.myNewMethod = function() { /* you can still use
'this'; i.e. jQuery(this) */ };

I hope I have made it more clear than hazy ;)

-Trey



On Mar 29, 2:01 am, Howard Rauscher <[email protected]> wrote:
> I tried to do something like this a couple of months ago, and you
> really can't do it efficiently.  This method is the only real way to
> do it
>
> "$('div ul').myNamespace().myMethod();"
>
> And it is pretty messy.
>
> If you really wanted to do it this way "$('div
> ul').myNamespace.myMethod()," you would have to bind the scope of each
> $ call to each method that is apart of your myNamespace object.
>
> You could also make you own static method that uses a jQuery object
> with your methods.
>
> $.myNamespace = function(selector) {
>   this.instance = $(selector);};
>
> myNamespace.prototype = {
>   myMethod : function() {
>     //do what ever
>     // this.instance will be the jQuery object
>   }
>
> };
>
> On Mar 28, 7:58 am, Julian Aubourg <[email protected]> wrote:
>
> > I don't think it is possible. No matter how you turn it around, you'll lose
> > the object context.
>
> > I suppose your myNamespace() function is something like:
>
> > $.fn.myNamespace = function() {
> >   return this;
>
> > }
>
> > right?
>
> > 2009/3/28 iceman2g <[email protected]>
>
> > > I asked this elsewhere and it was suggested that I ask here.
>
> > > So here goes
>
> > > I was curious if it's possible to extend jquery to do something along
> > > the
> > > lines of "$('div ul').myNamespace.myMethod();"?
>
> > > So far the closet I've seen anyone come to this, is along the lines of
> > > "$('div ul').myNamespace().myMethod();".
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to