如何从回调函数访问其他参数

How to access other arguments from callback function

我正在使用 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?

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

(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)))))))