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
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.
5 Comments
.apply(), not .call()for that particular situation.return this; to keep jq chainingreturn; in his code, I just copied that.return; would be more relevant, that's true.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
this will not be correct when the old one is called this way..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..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 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);
}