简体   繁体   English

扩展功能javascript的功能

[英]Extend functionality of function javascript

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. 诀窍是我需要将if语句动态添加到函数中,具有Plugin函数的代码会经常更新,并且我不想在每次文件更改时都重新添加该逻辑。 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. 我认为我可能需要对原型做一些事情,但是我对原型的理解仍然非常有限。

If Plugin is directly written in some third party code, you can rename and replace the old function: 如果Plugin是直接用某些第三方代码编写的,则可以重命名并替换旧功能:

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: 当然,所有分配给OldPlugin引用OldPlugin必须替换:

$.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. 如果Plugin不是直接写在任何地方,而是仅通过$.fn.validator ,那么我将保持原始功能不变,只需将新功能分配给$.fn.validator ,您就可以在其中调用原始的Plugin函数。

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 如果您需要新功能使用原始名称,则可以执行@Siguza回答

I think if you use JsonNode it will be more easy try doing this way 我认为如果您使用JsonNode,尝试这样做会更容易

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM