javascript - 将对象传递给 JS 函数

标签 javascript jquery

在这种情况下,如何将对象作为变量传递给函数?我收到 console.log(setup) = undefined?有没有更好的方法将回调函数传递给 getTemplate() ?谢谢

函数模板

function getTemplate (name, callback, dir) {
          if (dir === undefined) dir = ''
          var xhr = new XMLHttpRequest()
          var url = '/core/templates/' + dir + name + '.html'
          xhr.open('GET', url, true)
          xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
              var raw = xhr.responseText
              var compiled = Handlebars.compile(raw)
              callback(compiled)
            }
          }
          xhr.send()
        }

调用函数

for (var i = 0; i < item.length; i++) {
 var tabActive = ''
 if (i == 0) { tabActive = 'active' }

 var tab = item[i],
 tabId = tab.id,
 tabTitle = tab.title,
 variables = {tabActive: tabActive, tabId: tab.id, tabTitle: tab.title}

 console.log(variables) // *A increments 5328, 5329

 getTemplate('tab-nav', function (tmp) {

 console.log(variables) // *B increments 5328, 5328

 $(tabNavigationId + '>ul').append(tmp(variables))

 })

}

*A

index.js:18 Object {tabActive: "active", tabId: "5327", tabTitle: "User Experience"}
index.js:18 Object {tabActive: "", tabId: "5328", tabTitle: "Design"}
index.js:18 Object {tabActive: "", tabId: "5329", tabTitle: "Web Development"}
index.js:18 Object {tabActive: "", tabId: "5330", tabTitle: "Mobile Development"}

*B

index.js:21 Object {tabActive: "", tabId: "5743", tabTitle: "Extension Settings"}
index.js:21 Object {tabActive: "", tabId: "5743", tabTitle: "Extension Settings"}
index.js:21 Object {tabActive: "", tabId: "5743", tabTitle: "Extension Settings"}
index.js:21 Object {tabActive: "", tabId: "5743", tabTitle: "Extension Settings"}

从 getTemplate 'tmp' 回调

function ret(context, execOptions) {
        if (!compiled) {
          compiled = compileInput();
        }
        return compiled.call(this, context, execOptions);
      }

最佳答案

您无法在回调函数中传递变量。

您需要将其用作

var setup = {tabActive: tabActive, tabId: tab.id, tabTitle: tab.title}

getTemplate('tab-nav', function (tmp) {
  //setup is accessible here
  console.log(setup)
  $(tabNavigationId + '>ul').append(tmp(setup))
})

-- 新更新--

你的 getTemplate 函数function getTemplate (name,callback,dir){ 期望第二个参数是回调函数。 所以你需要做的是,使用 getTemplate 函数作为

getTemplate('tab-nav', myCallBackFunc, 'dir');

那么myCallBackFunc将是这样的。

我可以看到您正在使用一些参数调用回调。

function myCallBackFunc(response) {
 //do something with response
}

此外,您还没有定义有关 tmp 函数的任何内容

关于javascript - 将对象传递给 JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43012652/

相关文章:

javascript - 如何在div中的几个隐藏字段中获得1个隐藏字段

php - Jpgraph:如何手动设置 X 轴和 Y 轴的范围

javascript - 使 jqGrid 多选选择在分页、工具栏搜索或过滤器之后持续存在

javascript - 在链接上模拟 jQuery/JavaScript 中的点击

jquery - tinymce 删除文本空格和换行符

javascript - 当我运行sequelize.sync()时,为什么我的表没有被更改?

javascript - 创建缩放功能,当我们点击签名时整个页面会缩放,当我们点击 - 签名时会缩小。

javascript - 空字符串的长度怎么可能大于 0?

javascript - react : State not rendering in real time

javascript - jQuery 不追加对象值