【问题标题】:How to access other arguments from callback function如何从回调函数访问其他参数
【发布时间】:2016-11-11 22:28:14
【问题描述】:

我正在使用emacs-request 从网络获取一些 json 数据。这是一个例子

(defun test (arg1 arg2)
  (request
   "http://httpbin.org/get"
   :params '(("key" . "value") ("key2" . "value2"))
   :parser 'json-read
   :success (cl-function
             (lambda (&key data &allow-other-keys)
               (message "I sent: %S" (assoc-default 'args data))))))

我想知道:success等回调函数如何访问arg1和arg2?

【问题讨论】:

    标签: elisp


    【解决方案1】:

    您可以将lexical-binding variable 设置为t,允许lambda 访问外部函数的参数,或者将:success 函数包装在绑定外部函数参数的lexical-let 中:

    (defun test (arg1 arg2)
      (request
       "http://httpbin.org/get"
       :params '(("key" . "value") ("key2" . "value2"))
       :parser 'json-read
       :success (lexical-let ((arg1 arg1) (arg2 arg2))
                  (cl-function
                   (lambda (&key data &allow-other-keys)
                     (message "%s %s sent: %S" arg1 arg2 (assoc-default 'args data)))))))
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2019-12-18
      • 2014-06-23
      • 2018-03-07
      • 2015-07-11
      • 2021-09-14
      • 2018-10-16
      • 2013-09-13
      相关资源
      最近更新 更多