在我正在研究的混合移动应用程序中,我无法从外部类调用函数或方法。我已将代码简化为:
var hub_api = (function() {
function send() {
console.log('hub api called');
};
return {
send: function() {
send();
}
};
});
这样称呼:
(function() {
function register_event_handlers() {
$(document).on(" touchend", "#btntest", function(evt) {
hub_api.send();
alert('pressed test button');
});
}
document.addEventListener("app.Ready", register_event_handlers, false);
})();
实际代码在另一个项目中工作正常。这些脚本都包含在内:
<script type="application/javascript" src="js/helpers/api.js"></script>
<script type="application/javascript" src="js/helpers/testclass.js"></script>
运行该应用会返回hub_api is not defined
。我完全难过了。任何帮助表示赞赏。
编辑:我发现当我将hub_api函数放在app.js中时,它按预期工作,但在包含在单独的api.js脚本中时仍然没有。脚本以正确的顺序调用。
答案 0 :(得分:0)
尝试在主hub_api
中定义function()
,如下所示:
(function() {
var hub_api = (function() {
function send() {
console.log('hub api called');
};
return {
send: function() {
send();
}
};
})();
function register_event_handlers() {
$(document).on(" touchend", "#btntest", function(evt) {
hub_api.send();
alert('pressed test button');
});
}
document.addEventListener("app.Ready", register_event_handlers, false);
})();