Javascript 从类调用函数
在我正在开发的混合移动应用程序中,我不能从外部类调用函数或方法。我已将代码简化为:Javascript 从类调用函数,javascript,class,oop,mobile,intel-xdk,Javascript,Class,Oop,Mobile,Intel Xdk,在我正在开发的混合移动应用程序中,我不能从外部类调用函数或方法。我已将代码简化为: var hub_api = (function() { function send() { console.log('hub api called'); }; return { send: function() { send(); } }; }); 这就是所谓的: (function() { fu
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>
(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);
})();
实际代码在另一个项目中运行良好。脚本都是这样包含的:
(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>
(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);
})();
运行应用程序返回未定义的hub\u api
。我完全被难住了。感谢您的帮助
编辑:我发现,当我将hub_api函数放入app.js中时,它可以按预期工作,但当包含在单独的api.js脚本中时,它仍然不能工作。按正确的顺序调用脚本 尝试在主
函数()中定义hub\u api
,如下所示:
(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>
(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);
})();
当从那些事件处理程序外部调用时,它能工作吗?我不明白这个问题。“事件处理程序”只是一个函数。事件处理程序代码位于一个独立的(function(){…})()中代码>,所以外部的所有定义都不能在内部访问,所以你必须在内部定义它,这是不正确的。在外壳外部但在同一脚本文件中声明的hub_api可以正常工作。它也适用于不同的项目。它必须是如何加载脚本。