我想从导出的js文件访问功能。 等我的主要app.js文件顶部我需要这个文件是这样的:要求范围之间所需的功能
var api = require("./plugins/apis.js”);
我可以调用函数从该文件而不是内部的其他变种 功能,就算我送的API像参数
/**
* @param access_token your access token from your instance settings page
* @param [options] json object to be passed to the external web service. Can include any of 'context', 'verbose', 'n'
* @param callback callback that takes 2 arguments err and the response body
*/
var getData = function (access_token, options, callback) {
if(!callback) {
callback = options;
options = undefined;
}
// do stuff
}
function init(api) {
var information = getData(ACCESS_TOKEN, function (err, res) {
init(api)
// do stuff, but calls from apis.js functions not available.
我不想要这个递归,但我需要这里面我的getData函数,而不是超出范围。 我不知道如何使用回调。有人可以解释如何使用回调函数,并且可以同时使用外部apis.js函数中的函数吗?
你得到,如果你从调用'require'删除'.js'同样的问题?这是'app.js'里面的所有代码吗? – csum
我没有看到这样做的问题。我们需要更多的信息才能理解你为什么会得到这种行为。你能提供getData函数和api.js模块的实现吗? – jahnestacado