【问题标题】:Function is not defined (even though it is)未定义函数(即使已定义)
【发布时间】:2016-10-30 10:00:03
【问题描述】:

我在 .then promise 中调用这个函数

this.retrieveMatches();

这个函数如下所示

retrieveMatches: function(){
    var tableResult = makeHTMLMatchesTable(fetchMatches());
    var matches = document.getElementById('matches')
    matches.parentNode.insertBefore(tableResult, matches);
  },

这个函数就在它上面:

 makeHTMLMatchesTable: function(array){
    //does stuff
    }

那么为什么控制台会这样说:

Uncaught (in promise) ReferenceError: makeHTMLMatchesTable is not defined

什么时候明确定义?

【问题讨论】:

  • 呃,你不是用this来调用它。
  • 你应该包括所有定义的闭包
  • @Mayday “关闭”是什么意思?
  • 这个问题需要更多上下文:请在一个块中提供代码,以便不同对象在上下文和范围方面如何相互关联。
  • @tricot 实际上很清楚问题出在哪里:就像 vlaz 的评论一样。这个问题应该作为一个错字关闭,我们都继续前进。

标签: javascript function reactjs undefined


【解决方案1】:

更改此代码

retrieveMatches: function(){
    var tableResult = makeHTMLMatchesTable(fetchMatches());
    var matches = document.getElementById('matches')
    matches.parentNode.insertBefore(tableResult, matches);
  },

retrieveMatches: function(){
    var tableResult = this.makeHTMLMatchesTable(fetchMatches());
    var matches = document.getElementById('matches')
    matches.parentNode.insertBefore(tableResult, matches);
  }.bind(this),

【讨论】:

    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 2021-11-24
    • 2019-04-08
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多