微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-如何在jQuery中的变量中调用函数分配

我声明一个函数并将其分配给一个变量,该函数内部还有其他函数.通常,在页面首次加载时会加载函数等.当我在ajax成功回调中使用变量时,它说init()函数不是函数.

这是分配给名为GRID的变量的示例jquery函数

    var Grid = (function() {
       function init( config ) { ... }
         .
         .
         .
       function addItems( $items ) { ... }
      return { init: init, . . . addItems: addItems }
    }

();

我有一个这个ajax电话

     function updateItems{
      .
      .
      .
      .
       jQuery.ajax({

            type: 'get',
            url:  ajaxURL,
            data: ajaxData,
            dataType: 'json',

            //Ajax call is successful
            success: function ( response ) {
                //Add new posts
               Grid.init();

            },
            error: function () {

            }
        });
        .
        .
        .
        }

代码有什么问题?为什么检查器会返回一个错误,指出Grid.init()不是函数?请帮忙

解决方法:

var Grid = (function() {
   function init( config ) { ... }
     .
     .
     .
   function addItems( $items ) { ... }

   // explicitly return the functions, or properties you want to be attached to `Grid`
   return {
       init: init,
       .
       .
       .
       addItems: addItems
   }
// assuming you want this to be immediately invoked function expression
})();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐