search

Home  >  Q&A  >  body text

javascript - jquery绑定事件handler传入一个表达式?


如图所示,handler参数这里传入了一个表达式 ,这个是什么意思?

PHP中文网PHP中文网2894 days ago212

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-10 17:42:59

    $(document).on("click", (this._mouseclick = function(e) { }));
    
    // 等价于
    this._mouseclick = function(e) { };
    $(document).on("click", this._mouseclick);

    就是把两句话写成了一句话。

    this._mouseclick 现在看来是没有什么用,但是如果需要调用 off() 来去掉某个事件处理函数的时候,就比较有用了,比如

    {
        bind: function() { },
        unbind: function() {
            $(document).off("click", this._mouseclick);
        }
    }

    reply
    0
  • 阿神

    阿神2017-04-10 17:42:59

    等价于:

    var obj = {
        _mouteclick: function(e) {
    
        },
        bind: function() {
            $(document).on('click', this._mouteclick);
        }
    }

    可能obj._mouteclick什么时候会拿出来用,但好像又没啥特别意义。

    reply
    0
  • PHP中文网

    PHP中文网2017-04-10 17:42:59

    谢邀,赞一下答案

    reply
    0
  • 黄舟

    黄舟2017-04-10 17:42:59

    1楼的答案很详细了。我第一反应也是off可能会用到。
    就跟定时器命名一样,清除定时器的时候会用到这个命名。

    reply
    0
  • Cancelreply