javascript - 在 JavaScript 中使用 ()() 将参数传递给匿名函数

标签 javascript

我正在尝试向对象中的每个函数添加一个钩子(Hook),以下是我的代码,并且运行良好。

    function foo(){}
    foo.beforeMethodHook = function(){console.log('Hook was called.');}
    foo.getInstance = function(){
        var newInstance = new foo;
        var funcNames = Object.getOwnPropertyNames(foo);
        for(i in funcNames){
            var funcName = funcNames[i];
            if(funcName == 'getInstance' || funcName == 'beforeMethodHook' || Object.hasOwnProperty(funcName)) continue;
            newInstance[funcName] = function (){
                foo.beforeMethodHook();
                return foo[this].apply(foo,arguments);
            }.bind(funcName);
        }
        return newInstance;
    }
    foo.test1 = function(arg1){console.log('test1 was called. arg1 = ' + arg1);return true;}
    foo.test2 = function(arg1,arg2){console.log('test2 was called. arg1 = ' + arg1 + ' , arg2 = ' + arg2);return true;}
    //Test
    var f = foo.getInstance();
    f.test1('ahaha');
    f.test2('heihei','houhou');

由于IE10-不支持function(){}.bind(),我尝试将.bind()更改为(function(){})(),如下

    newInstance[funcName] = (function (){
        foo.beforeMethodHook();console.log(arguments);
        return foo[funcName].apply(foo,arguments);
    })(funcName);

但问题来了,我失去了f.test1('ahaha')已经通过的论点。参数数组仅给出 ["test1"],即函数名称。

我该如何解决这个问题?提前致谢。

最佳答案

您可以实现自己的绑定(bind)。简易版:

if (!Function.prototype.bind) {
    Function.prototype.bind = function(that) {
        var fn = this;
        return function() {
            fn.apply(that, arguments);
        }
    };
}

或正确的版本:

if (!Function.prototype.bind) {
  Function.prototype.bind = function (oThis) {
    if (typeof this !== "function") {
      // closest thing possible to the ECMAScript 5 internal IsCallable function
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
    }

    var aArgs = Array.prototype.slice.call(arguments, 1), 
        fToBind = this, 
        fNOP = function () {},
        fBound = function () {
          return fToBind.apply(this instanceof fNOP && oThis
                                 ? this
                                 : oThis,
                               aArgs.concat(Array.prototype.slice.call(arguments)));
        };

    fNOP.prototype = this.prototype;
    fBound.prototype = new fNOP();

    return fBound;
  };
}

代码取自:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

关于javascript - 在 JavaScript 中使用 ()() 将参数传递给匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19679676/

相关文章:

javascript - 根据鼠标滚轮移动滑动菜单栏

javascript - 为什么 Array.splice() 这么慢?

javascript - jQuery 单击添加到图像的 src

javascript - 在 ajax 调用和 html 呈现之后 Angular 访问数据

从文本中删除 bb_quotes ([quote..] [/quote]) 的 Javascript

javascript - CSS 和 Javascript/jQuery - 类似于 Facebook 的可折叠 DIV 部分

javascript 添加和删除输入值

javascript - html/php 表单转 JSON 的问题

Javascript 图像 slider 将文本发送到屏幕外并调出页脚

javascript - 打印后从 JavaScript 数组中删除逗号