我声明一个函数并将其分配给一个变量,该函数内部还有其他函数.通常,在页面首次加载时会加载函数等.当我在ajax成功回调中使用变量时,它说init()函数不是函数.
var Grid = (function() {
function init( config ) { ... }
.
.
.
function addItems( $items ) { ... }
return { init: init, . . . addItems: addItems }
}
();
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] 举报,一经查实,本站将立刻删除。