Javascript 如何访问拥有function.prototype扩展的函数?

Javascript 如何访问拥有function.prototype扩展的函数?,javascript,prototype,Javascript,Prototype,我试图创建一个函数扩展来消除任何函数(如果函数被快速连续调用多次,则只执行一次,并以最佳方式返回缓存值) 我打算在UI框架中使用它,但我希望它是可移植的。我目前掌握的守则如下: Function.prototype.debounce = function() { var originalFunction = this; //this should be 'clickButton' in the example implementation var originalArgument

我试图创建一个函数扩展来消除任何函数(如果函数被快速连续调用多次,则只执行一次,并以最佳方式返回缓存值)

我打算在UI框架中使用它,但我希望它是可移植的。我目前掌握的守则如下:

Function.prototype.debounce = function()
{
    var originalFunction = this; //this should be 'clickButton' in the example implementation
    var originalArguments = arguments;

    function debouncedFunction()
    {
        var originalContext = this;
        return originalFunction.apply(originalContext,originalArguments)
    }

    if(this.__DEBOUNCEDVALUE === undefined)
    {
        this.__DEBOUNCEDVALUE = debouncedFunction();
        if(this.__DEBOUNCEDVALUE === undefined)
            this.__DEBOUNCEDVALUE = null;
        setTimeout(function(){originalFunction.__DEBOUNCEDVALUE = undefined},1000);
    }

    return this;
}
接下来,我定义了一个通用函数“clickButton”,如下所示:

function clickButton()
{
    document.getElementById('log').innerHTML += "<br/>Clicked "+arguments[1];
    return "some value";
}

这只能通过将函数对象绑定到去Bounce的某种方法来实现

一种方法是使用
.bind

buttons[i].addEventListener('click',clickButton.debounce.bind(clickButton)),

另一种方法是传递一个匿名函数,该函数关闭
clickButton

buttons[i].addEventListener('click',function(e) {
    return clickButton.debounce.apply(clickButton, arguments);

        // The following would suffice for this example:
    // return clickButton.debounce(e);
}),

但是除了这些技术之外,
debounce
在传递给
addEventListener
时,将没有引用对象的内存

buttons[i].addEventListener('click',function(e) {
    return clickButton.debounce.apply(clickButton, arguments);

        // The following would suffice for this example:
    // return clickButton.debounce(e);
}),