首页 > 解决方案 > 致敬.js中这个回调参数是什么意思?

问题描述

js新手。我正在开发一个个人网站,该网站使用贡品.js 具有@mention 功能就我而言,我需要从远程服务器检索提及列表。官方文档给出了一个实现的例子。令我困惑的是cb参数的含义,它甚至没有在任何地方定义。谁能帮忙解释一下?

{
  //..other config options
  // function retrieving an array of objects
  values: function (text, cb) { 
    remoteSearch(text, users => cb(users));
  },
  lookup: 'name',
  fillAttr: 'name'
}
// ajax
function remoteSearch(text, cb) {
  var URL = "YOUR DATA ENDPOINT";
  xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) {
        var data = JSON.parse(xhr.responseText);
        cb(data);
      } else if (xhr.status === 403) {
        cb([]);
      }
    }
  };
  xhr.open("GET", URL + "?q=" + text, true);
  xhr.send();
}

标签: javascriptcallbackfrontend

解决方案


含义隐藏在您的问题的标题中,cb意味着回调:)

它被函数调用,而该remoteSearch函数又被调用了cb由贡品.js引擎传递的参数。


推荐阅读