Javascript function hooks

后端 未结 7 994
孤街浪徒
孤街浪徒 2020-12-24 15:17

EDIT: OK, I believe the following solutions are valid:

  1. Use the jQuery AOP plugin. It basically wraps the old function together with the hook into a function

相关标签:
7条回答
  • 2020-12-24 15:50

    This answer is not definitive, but rather demonstrative of a different technique than those offered thus far. This leverages the fact that a function in Javascript is a first-class object, and as such, a) you can pass it as a value to another function and b) you can add properties to it. Combine these traits with function's built-in "call" (or "apply") methods, and you have yourself a start toward a solution.

    var function_itself = function() {
        alert('in function itself');
    }
    function_itself.PRE_PROCESS = function() {
        alert('in pre_process');
    }
    function_itself.POST_PROCESS = function() {
        alert('in post_process');
    }
    
    var function_processor = function(func) {
        if (func.PRE_PROCESS) {
            func.PRE_PROCESS.call();
        }
        func.call();
        if (func.POST_PROCESS) {
            func.POST_PROCESS.call();
        }        
    }
    
    0 讨论(0)
  • 2020-12-24 15:52

    Very simple answer:

    function someFunction() { alert("Bar!") }
    var placeholder=someFunction;
    someFunction=function() {
      alert("Foo?");
      placeholder();
    }
    
    0 讨论(0)
  • 2020-12-24 15:53

    The following function will give you before and after hooks that can be stacked. So if you have a number of potential functions that need to run before the given function or after the given function then this would be a working solution. This solution does not require jQuery and uses native array methods (no shims required). It should also be context sensitive so if you are calling the original function with a context if should run each before and after function likewise.

    // usage: 
    /*
    function test(x) {
        alert(x);
    }
    
    var htest = hookable(test);
    
    htest.addHook("before", function (x) {
        alert("Before " + x);
    })
    htest.addHook("after", function (x) {
        alert("After " + x);
    })
    
    htest("test") // => Before test ... test ... After test
    */
    function hookable(fn) {
        var ifn = fn,
            hooks = {
                before : [],
                after : []
            };
    
        function hookableFunction() {
            var args = [].slice.call(arguments, 0),
                i = 0,
                fn;
            for (i = 0; !!hooks.before[i]; i += 1) {
                fn = hooks.before[i];
                fn.apply(this, args);
            }
            ifn.apply(this, arguments);
            for (i = 0; !!hooks.after[i]; i++) {
                fn = hooks.after[i];
                fn.apply(this, args);
            }
        }
    
        hookableFunction.addHook = function (type, fn) {
            if (hooks[type] instanceof Array) {
                hooks[type].push(fn);
            } else {
                throw (function () {
                    var e = new Error("Invalid hook type");
                    e.expected = Object.keys(hooks);
                    e.got = type;
                    return e;
                }());
            }
        };
    
        return hookableFunction;
    }
    
    0 讨论(0)
  • 2020-12-24 15:56

    Here's what I did, might be useful in other applications like this:

    //Setup a hooking object
    a={
        hook:function(name,f){
            aion.hooks[name]=f;
        }
    }a.hooks={
        //default hooks (also sets the object)
    }; 
    
    //Add a hook
    a.hook('test',function(){
        alert('test');
    });
    
    //Apply each Hook (can be done with for)
    $.each(a.hooks,function(index,f){
        f();
    });
    
    0 讨论(0)
  • 2020-12-24 15:58

    I don't know if this will be useful. You do need to modify the original function but only once and you don't need to keep editing it for firing hooks

    https://github.com/rcorp/hooker

    0 讨论(0)
  • 2020-12-24 15:59

    Might not be pretty but it seems to work...

    <script>
    
    function A(x) { alert(x); return x; }
    function B() { alert(123); }
    
    function addHook(functionB, functionA, parent)
    {
        if (typeof parent == 'undefined')
            parent = window;
    
        for (var i in parent)
        {
            if (parent[i] === functionA)
            {
                parent[i] = function()
                {
                    functionB();
                    return functionA.apply(this, arguments)
                }
    
                break;
            }
        }
    }
    
    addHook(B, A);
    
    A(2);
    
    </script>
    
    0 讨论(0)
提交回复
热议问题