views:

135

answers:

2

I'm quite new to javascript but have undertaken a task to get better aquainted with it. However, I am running into some problems with jQuery. The following javascript is the code that is in a third party jQuery plugin and I would love to be able to override the funFunction() function here to do my own implementation. Is this possible, if so, how can I do it? I've been doing a fair amount of searching and have tried a number of methods for overriding the function using things like:

jQuery.blah.funFunction = funtion() { alert("like this"); };

Main code:

(function($) { 
    $.extend( {
        blah: new function() { 
          this.construct =  = function(settings) {
              //Construct... stuff
          }; 
          function funFunction() {
            //Function I want to override
          } 
        } 
    });
})(jQuery);

For those further interested I am trying to override tablesorter so that the only way a user can sort a column is in ascending order only.

Edit:

There is a wordpress installation that uses WP-Table-Reloaded which in turn uses this plugin. I don't want to change the core code for this plugin because if there was ever an update I would then have to make sure that my predecessor knew exactly what I had done.

I've been programming for a long time and feel like I should easily be able to pick up javascript whilst also looking at jQuery. I know exactly what I need to do for this, just not how I can override this function.

+1  A: 

The nice thing about jQuery is that if a plugin doesn't quite do what you want, you can

  • Look for another plugin that does the same thing;
  • Grab the plugin, and if it's GPL just fix it

My experience is that most plugins are pretty good, but they're not written by deities so sometimes there are problems or sometimes what you need just happens to be askew from what the designer anticipated.

Now: you really should do some serious thinking about what you want to do and what your needs are, and as a newbie what sort of assumptions about the "right way to do things" that you're bringing to the party. Every programming environment has its own personality, which often is almost a cultural entity. Things may seem weird, or bad, or ugly, but as an outsider you should suspend judgment until you get really used to all the idioms.

Pointy
Hey, I appreciate your comment, I will add a little more information to the end as to why I don't want to rewrite the plugin.
PintSizedCat
@PintSizedCat - Whatever your preferences is...unfortunately it doesn't matter in this case, you can't edit what you want to edit without doing it either: a) **inside** the plugin or b) another plugin, possibly your own.
Nick Craver
Ok, I'll leave it open incase someone finds some fantastic way of doing it ;)
PintSizedCat
A: 

Who cares about updates of minor libraries anyway? As long as the used version does what is needed and have no applicable bugs there is no gain from upgrading.

In any case, I would never recommend retrieving an accessible parent function as a string like this: functionAsString=""+functionName edit what needs editing and then regenerate the function: functionName=eval("("+functionAsString+")"). But it is possible to do so.

Different browser return slightly different results when you convert a function to string, but they are all valid declarations for that exact function, so you should be able to regex your way out of the differences.

Alternately you could retrieve the script file using an ajax call, and then do the same as above. I still wouldn't recommend it though.

eBusiness