简介:
我知道“这段代码是如何工作的?”类型的问题是不受欢迎的,我会看起来像砖头一样聪明地阅读“太阳”来问这样的问题但是......这里是。
我正在尝试理解 JavaScript 中的原型(prototype)设计,现在这不是问题所在,我理解原型(prototype)设计结构的基础知识,因为您编写一个函数,然后通过使用原型(prototype)扩展该函数的参数。
(是的,在我火上浇油之前,我已经通读了社区维基百科并在 SO 上发布了关于这个特定主题的帖子,所以不要只是把我骗到他们那里,而且我已经阅读了 John Reisg 的笔记这个主题也有很大帮助。(最令人困惑的方面是理解 this
及其 DOM 引用方法。))
但事情是这样的:
我为 SO API 编写了一个简单的 API 解析器,它可以提取各种数据,我对自己和我第一次涉足 JS 感到高兴 我在 SO JS Chatroom 中发布了一个链接,看看他们是否认为这可以做得更多高效并且@IvoWetzel 建议我更改为原型(prototype)包装器来创建 API URL 查询,所以我调查了它并且他根据我的代码发布了这个示例代码:
//API Handling for asynchronicity
function API(site, key, ...) {
this.site = site;
this.key = key;
this.schedule = new Scheduler(this);
}
API.prototype = {
lookup: function(resource, callback) {
// build the url etc here
this.request(url, callback);
},
request: function(url, callback) {
// build a request here and send it
request.on('finished', function() {
callback();
});
}
};
function Scheduler(api) {
return function(method, options, interval) {
var id = null;
function request() {
api[method](options...);
id = setTimeout(function() {
request();
}, interval);
}
return {
stop: function(attribute) {
clearTimeout(id);
}
}
}
}
显然这还没有完成,只是一个 shell,但老实说,除了顶部的 API
函数,我不知道代码是如何工作的,尤其是 lookup :
、request:
以及 return function
如何可以在另一个函数中包含未在任何地方定义甚至未传递给它的变量!
Scheduler
函数让我感到困惑...
结论:
所以,有人可以用简单的术语解释一下(想想解释为什么不把口琴放在马桶里给 3 岁的 child )上面的代码如何与 my GitHub repo 中的代码执行相同的操作? (第 176-210、243-245 和 277-365 行)。
注意:如果你说使用 JQuery.parseJSON/libraryX,我将把它作为一个 JS 学习练习来做。无论我会剥削你什么 :)
谢谢大家!
最佳答案
让我们把它分解成几个部分
//create function API which is meant to be instantiated into an object using
///var foo = new API();
function API(site, key, ...) {
this.site = site;
this.key = key;
this.schedule = new Scheduler(this);
}
//create two prototype functions on the API function called lookup & request
//these two functions will be available as public functions on all
//instantiated API objects and can be called like this
//foo.lookup(resource, callback); / foo.request(url, callback);
API.prototype = {
lookup: function(resource, callback) {
// build the url etc here
this.request(url, callback);
},
request: function(url, callback) {
// build a request here and send it
request.on('finished', function() {
callback();
});
}
};
//define function Scheduler
function Scheduler(api) {
//when called, imidiately return a reference to an
//anonymous function that can be called later with
//the three arguments method, options & interval
return function(method, options, interval) {
// define a local variable id for this anonymous function
var id = null;
//create a private function inside the anonymous function call request
function request() {
//private function requests internals
api[method](options...);
id = setTimeout(function() {
request();
}, interval);
}
//when anonymous function is called return
//an object with a function called stop as property
return {
stop: function(attribute) {
clearTimeout(id);
}
}
}
}
最后你会做这样的事情:
var foo = new API();
var scheduler = foo.schedule('lookup', {some options object I presume}, some_interval);
scheduler.stop();
关于javascript - 原型(prototype)和嵌套返回函数,帮助!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130082/