Yes, Raine, that would certainly work. A couple of notes... First, 'default' is a reserved word in JavaScript. You can use it as a method name if you want, but not using foo.default notation. That may work in some browsers, but you can't count on it working in all of them. You would have to use foo['default'] instead:
$.fn.response.redirect['default'] = function (){/*...*/}; $.fn.response.redirect['default'](); Or simply pick a different name that isn't a reserved word. Second, do these public functions care if they are called as methods of the $.fn.response.redirect object? IOW, do they use "this" and expect it to be a reference to $.fn.response.redirect? If they don't, you can simplify the code a bit: var fn = $.fn.response.redirect[response.redirect_type] || $.fn.response.redirect['default']; fn(); Or even: ( $.fn.response.redirect[response.redirect_type] || $.fn.response.redirect['default'] )(); In fact, if you are providing the response.redirect_type in JSON that you're generating yourself, and you know that it will be either a valid function name or undefined, you could just do: $.fn.response.redirect[ response.redirect_type || 'default' ](); Of course, there's nothing wrong with the code as you've written it (except for the .default part as mentioned), it's just nice to have some options. (I should mention that these bits of code don't work *exactly* the same as yours - you are doing a strict test for undefined, while these use the || operator and therefore just test for any "false" value which would include undefined, null, false, 0 or "".) -Mike > From: jquery-en@googlegroups.com > > Thanks Mike, > > I'm following this tutorial here: > http://www.learningjquery.com/2007/10/a-plugin-development-pattern > > I have several public functions like this: > > $.fn.response.redirect.default = function (){//something here}; > $.fn.response.redirect.type1 = function (){//something here}; > $.fn.response.redirect.type2 = function (){//something here}; > > I wanted to be able to call the appropriate function depending on a > string passed back to me via json > > Im thinking of doing it like this (based on your example): > > if($.fn.response.redirect[response.redirect_type] !== undefined) > $.fn.response.redirect[response.redirect_type](); > else > $.fn.response.redirect.default(); > > Do you think it would work? > > Thanks again for your help > > Raine > > On Jan 2, 3:34 am, "Michael Geary" <m...@mg.to> wrote: > > I don't know about the $.fn.myplugin part - that's not how you would > > typically call a plugin function in the first place. > > > > But in general, given any object 'foo' and any property > 'bar', you can > > reference the 'bar' property in either of two ways: > > > > foo.bar > > > > Or: > > > > foo['bar'] > > > > If 'bar' is a method, you can call it with: > > > > foo.bar(...); > > > > Or: > > > > foo['bar'](...); > > > > Either one means exactly the same thing. > > > > One common use for this is with show and hide methods. > Instead of coding: > > > > if( doShow ) > > $('#foo').show(); > > else > > $('#foo').hide(); > > > > You can code: > > > > $('#foo')[ doShow ? 'show' : 'hide' ](); > > > > -Mike > > > > > From:yellow1912 > > > > > Something like this > > > var func = 'myFunc'; > > > > > Can I call the function like this for example: > > > > > $.fn.myplugin.(func)(); > > > > > (I'm using this in a plugin I'm working on) > > > > > Thank you very much >