weixin_33738555 2015-03-02 10:31 采纳率: 0%
浏览 69

声明的函数未调用

I have a function to fetch my data from backend. once the data is loaded i am calling a function which is sent as callback. but i am getting error as undefined not a function

here is the code:

var initDataTable = function () {

      var getDableData = function (url,callback) {
            $.getJSON(url)
              .then(function (data) {
                callback(data);
            });
      };

      return {

        init : function (dataAssests) {
          this.container = dataAssests.container;
          this.headerNames = dataAssests.names;          
          getDableData(dataAssests.url, this.dataReceiver); //getting required data
        },

        dataReceiver : function (data) {
            this.tableData = data;
            this.tableMaker(); //not calling error Uncaught TypeError: undefined is not a function.
        },

        tableMaker : function () {
          console.log(this.tableData);
        }

      };
};

//tabular data starts...
var dataAssests = {
                    container: $('.dataTable'), 
                    url : 'https://tcs.firebaseio.com/d/DocPageDetails/d/Organizations.json', 
                    names : ['Organization Name', 'Zip Code', 'Telephone', 'Organization TypeName' ]
                  };

var dataTableFrist = initDataTable().init(dataAssests);
//tabular data ends...

jsfiddle

  • 写回答

1条回答 默认 最新

  • George_Fal 2015-03-02 10:37
    关注

    The problem is not that callback is not defined, it is inside dataReceiver where this is not referring to the object what you are expecting.

    Since you are invoking the callback without a context, this inside dataReceiver is referring to the window object so this.tableMaker is undefined thus the error.

    One possible solution is to use Function.bind() to pass a custom execution context to the callback method

    getDableData(dataAssests.url, this.dataReceiver.bind(this));
    

    Demo: Fiddle

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)