1

I have a function that is declared like this:

    function Plugin(option) {
       //logic here
    }
    $.fn.validator = Plugin

I need to extend this function with some logic so that it does something more like this:

    function Plugin(option) {
       if (myvariable == true)
       {
           //seperate logic
           return;
       }
       //logic here
    }

The trick is that I need to add that if statement into the function dynamically, the code that has the Plugin function gets updated frequently and I don't want to re-add that logic every time the file changes. As of right now I just create the whole function with all the logic and replace the old one with it. But this seems like a lot of wasted code. I was hoping there was a way to simply merge them, so I tried.

    function Plugin(option) {
       //logic here
    }
    function extendedPlugin(option) {
       //seperate logic here
    }
  $.fn.validator = $.extend(Plugin, extendedPlugin);
    //or
  $.fn.extend($.fn.validator, extendedPlugin)

I've tried a couple variations of that logic at the bottom but it just returns the first function every time, is there a way to merge them since functions are technically objects aren't they? I think I may need to do something with prototypes but my understanding of them is still very limited.

3 Answers 3

3

If Plugin is directly written in some third party code, you can rename and replace the old function:

var OldPlugin = Plugin;
Plugin = function(option)
{
    if (myvariable == true)
    {
        //seperate logic
        return /* anything? */;
    }
    return OldPlugin.apply(this, arguments);
}

Of course, any assigned references to OldPlugin must be replaced too:

$.fn.validator = Plugin; // After redefining "Plugin"

If Plugin is not directly written anywhere, but only called via $.fn.validator, then I would leave the original function intact and simply assign a new function to $.fn.validator, from where you can call the original Plugin function.

Sign up to request clarification or add additional context in comments.

5 Comments

You should be using .apply(), not .call()for that particular situation.
And I think you should return this; to keep jq chaining
@A.Wolff OP provided a return; in his code, I just copied that.
@Siguza ya but if original plugin returns matched set of elements, you should do the same, otherwise return; would be more relevant, that's true.
Yah the return was psuedocode the actual return would make no sense in this context. This worked great though, in my particular scenario I didn't have to save the original to the "OldPlugin" variable, I just added the Plugin.apply(this, arguments); at the end of extendedPlugin and overwrote validator, but same concept thanks! (will accept soon)
1

Is there a reason you can't simply call the old function from the extended one?

function Plugin(option) {
   //logic here
}
function extendedPlugin(option) {
   //seperate logic here
   // then call Plugin?
   return Plugin.call(this, option);
}

If you need the new function to have the original name, you can instead do what @Siguza answered

5 Comments

The value of this will not be correct when the old one is called this way.
@jfriend00 Well spotted, I guess apply() is needed to preserve it. Editing.
.apply() accepts an array as the 2nd argument. If you're going to just pass the argument itself, then you need .call(). And, you have to show how extendedPlugin gets hooked into jQuery in the first place. The answer as it stands is incomplete.
I corrected the .call() - as for attaching to jQuery, isn't it largely irrelevant how it is done? something like $.fn.pluginName = extendedPlugin should do the trick?
I don't know how you can say it's irrelevant. It's a required part of the solution.
0

I think if you use JsonNode it will be more easy try doing this way

function plugin(option){
   option.execute();
}

$.fn.validator = Plugin;

And implement in this way

var process1 = {
       "execute":function(){
           alert('hello proccess #1');
       }
      };
var process2 = {
       "execute":function(){
           alert('hello process #2');
       }
      };

$.fn.validator(process1);
$.fn.validator(process2);

Or

if(myvariable){
     $.fn.validator(process1);
}else{
    $.fn.validator(process2);
}

2 Comments

This implementation is a pattern design most used in software development. It's make it more open and you can change the behavior dynamically
You don't have to comment on your own answer, you can just edit it and append the information :) That said, I don't see how this answers the question about extending a function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.