Myl*_*ray 2 javascript json prototype return function
介绍:
我知道"这段代码是如何工作的?" 类型问题是不受欢迎的,我会看起来像读"太阳报"那样聪明地问这样一个问题,但是......这里就是这样.
我试图理解JavaScript中的原型,现在这不是问题,我理解原型结构的基础知识,你编写一个函数,然后通过使用原型扩展该函数的参数.
(是的,在我得到火焰之前,我已经通过社区维基阅读了关于这个特定主题的帖子,所以不要只是告诉他们,以及我已经通过John Reisg关于这个主题的说明也帮助了很多.(最令人困惑的方面是理解this
及其DOM引用方法.))
但事情就是这样:
我为SO API写了一个简单的API解析器,它可以提取各种数据,我自己和我第一次冒险进入JS我在SO JS Chatroom中发布了一个链接,看看他们是否认为这可以更有效地完成,并且@ IvoWetzel建议我更改为原型包装器来创建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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然这还没有完成,只有一个shell,但老实说除了API
顶部的函数我不知道代码是如何工作的,特别是lookup:
,request:
以及如何使用return function
未定义的变量在另一个内部任何地方甚至传递给它!
而这个Scheduler
功能让我感到困惑......
结论:
那么,有人可以用简单的术语解释(想想解释为什么不把口琴放入厕所给3岁的孩子)上面的代码如何与我的GitHub回购中的代码相同(第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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后你会做这样的事情:
var foo = new API();
var scheduler = foo.schedule('lookup', {some options object I presume}, some_interval);
scheduler.stop();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3133 次 |
最近记录: |