Javascript Node.js模块函数的访问参数
我对node.js完全陌生,对于我的问题,我找不到类似的问题。我相信这对你们中的一个来说很容易解决。。。至少我想是这样 我正在尝试使用node.js的npm mediawiki模块获取Wiki页面的特殊段落!我使用预定义函数获取段落,如下所示:Javascript Node.js模块函数的访问参数,javascript,node.js,callback,mediawiki-extensions,Javascript,Node.js,Callback,Mediawiki Extensions,我对node.js完全陌生,对于我的问题,我找不到类似的问题。我相信这对你们中的一个来说很容易解决。。。至少我想是这样 我正在尝试使用node.js的npm mediawiki模块获取Wiki页面的特殊段落!我使用预定义函数获取段落,如下所示: bot.page(title).complete(function (title, text, date) { //extract section '== Check ==' from wikipage&clean string
bot.page(title).complete(function (title, text, date) {
//extract section '== Check ==' from wikipage&clean string
var result = S(text).between('== Check ==', '==').s;
});
这很有效。我想要的是:在其他函数中使用代码块之外的“result”。我认为这与回调有关,但我不确定如何处理,因为这是mediawiki模块中预定义的函数
获取wikipage模块的示例函数如下所示:
/**
* Request the content of page by title
* @param title the title of the page
* @param isPriority (optional) should the request be added to the top of the request queue (defualt: false)
*/
Bot.prototype.page = function (title, isPriority) {
return _page.call(this, { titles: title }, isPriority);
};
它使用模块的以下功能:
function _page(query, isPriority) {
var promise = new Promise();
query.action = "query";
query.prop = "revisions";
query.rvprop = "timestamp|content";
this.get(query, isPriority).complete(function (data) {
var pages = Object.getOwnPropertyNames(data.query.pages);
var _this = this;
pages.forEach(function (id) {
var page = data.query.pages[id];
promise._onComplete.call(_this, page.title, page.revisions[0]["*"], new Date(page.revisions[0].timestamp));
});
}).error(function (err) {
promise._onError.call(this, err);
});
return promise;
}
还有一个完整的回调函数,我不知道如何使用它:
/**
* Sets the complete callback
* @param callback a Function to call on complete
*/
Promise.prototype.complete = function(callback){
this._onComplete = callback;
return this;
};
如何通过在模块函数之外使用回调来访问“result”变量?我不知道如何处理回调,因为它是模块的预定义函数
我想要的是:在其他函数中使用代码块之外的“result”
你不能。您需要在该代码块内使用结果(该代码块称为回调
函数btw)。您仍然可以将它们传递给其他函数,只需在回调函数中执行:
bot.page(title).complete(function (title, text, date) {
//extract section '== Check ==' from wikipage&clean string
var result = S(text).between('== Check ==', '==').s;
other_function(result); // <------------- this is how you use it
});
bot.page(标题)。完成(功能(标题、文本、日期){
//从wikipage和clean字符串中提取节“==检查==”
var result=S(text).between('==Check=','==').S;
其他_函数(结果);//可能重复的