我正试图让jQuery风格的函数链接在我的脑海中.我的意思是:
var e = f1('test').f2().f3();
Run Code Online (Sandbox Code Playgroud)
我有一个例子可以工作,而另一个没有.我会发布以下内容.我总是想学习第一个原理的基本原理,以便我可以在它之上构建.到目前为止,我对链接的工作方式只有一个粗略而宽松的理解,而且我遇到了一些我无法智能排除故障的错误.
我知道的:
这个例子有效:
var one = function(num){
this.oldnum = num;
this.add = function(){
this.oldnum++;
return this;
}
if(this instanceof one){
return this.one;
}else{
return new one(num);
}
}
var test = one(1).add().add();
Run Code Online (Sandbox Code Playgroud)
但是这个没有:
var gmap = function(){
this.add = function(){
alert('add');
return this;
}
if(this instanceof gmap) {
return this.gmap;
} else{
return new gmap();
}
}
var test = gmap.add();
Run Code Online (Sandbox Code Playgroud) 我如何设计一个API来隐藏AJAX和HTTP请求的异步性质,或者基本上将其延迟以提供流畅的界面.要在Twitter的新Anywhere API中显示示例:
// get @ded's first 20 statuses, filter only the tweets that
// mention photography, and render each into an HTML element
T.User.find('ded').timeline().first(20).filter(filterer).each(function(status) {
$('div#tweets').append('<p>' + status.text + '</p>');
});
function filterer(status) {
return status.text.match(/photography/);
}
Run Code Online (Sandbox Code Playgroud)
vs this(每个调用的异步性质清晰可见)
T.User.find('ded', function(user) {
user.timeline(function(statuses) {
statuses.first(20).filter(filterer).each(function(status) {
$('div#tweets').append('<p>' + status.text + '</p>');
});
});
});
function filterer(status) {
return status.text.match(/photography/);
}
Run Code Online (Sandbox Code Playgroud)
它找到用户,获取他们的推文时间轴,仅过滤前20条推文,应用自定义过滤器,并最终使用回调函数来处理每条推文.
我猜这样设计良好的API应该像查询构建器(想想ORM)一样工作,每个函数调用构建查询(在这种情况下为HTTP URL),直到它遇到循环函数,如每个/ map /等.进行HTTP调用,传入函数成为回调函数.
一个简单的开发途径是使每个AJAX调用同步,但这可能不是最好的解决方案.我有兴趣找出使其异步的方法,并仍然隐藏AJAX的异步性质.
我目前正在研究node.js应用程序,我遇到了常见的异步代码问题.
我正在Node的HTTP模块上实现一个服务服务器.
此服务器支持(表示类似)路由.例如,我的代码如下所示:
server.any("/someRoute",function(req,resp){
resp.end("this text is sent to clients via http")
});
Run Code Online (Sandbox Code Playgroud)
服务器需要能够承受故障,当传递给任何一个函数时出现问题时,我不想让整个服务器崩溃.当我编写的代码如下所示时会出现问题:
server.any("/someRoute",function(req,resp){
setTimeout(function(){
throw new Error("This won't get caught");
},100);
});
Run Code Online (Sandbox Code Playgroud)
我不知道我怎么可能在这里发现错误.我不想让服务器崩溃超过一个服务器端故障,而是我想服务500.
我能够提出的唯一解决方案实际上并不富有表现力.我只是process.on("uncaughtException",callback)使用节点0.8 来提出使用和类似的代码Domains(这是一种部分补救措施,但是域名目前是错误的,但由于我最终必须为每个句柄创建一个域,因此这仍然不是很有表现力).
我想要实现的是throw将函数绑定到作用域,理想的解决方案就像将所有抛出的错误从函数绑定到特定的处理函数.
这可能吗?在这种情况下处理错误的最佳做法是什么?
我想强调它应该能够在一个错误的请求后继续提供请求,并在每个请求上重新启动服务器或为每个处理程序创建域并捕获未捕获的异常对我来说似乎是一个坏主意.另外 - 我听说过承诺可以帮助我(throw承诺中的某些事情),在这种情况下可以承诺帮助我吗?
我正在阅读这篇文章,关于承诺抽象的部分对我来说似乎有点过于复杂.以下是一个例子:
requestSomeData("http://example.com/foo") // returns a promise for the response
.then(function(response){ // ‘then’ is used to provide a promise handler
return JSON.parse(response.body); // parse the body
}) // returns a promise for the parsed body
.then(function(data){
return data.price; // get the price
}) // returns a promise for the price
.then(function(price){ // print out the price when it is fulfilled
print("The price is " + price);
});
Run Code Online (Sandbox Code Playgroud)
在我看来,以下可以用更少的代码行提供相同的结果:
requestSomeData("http://example.com/foo")
.requestHandler(function(response){
// parse the body
var data = JSON.parse(response.body);
// …Run Code Online (Sandbox Code Playgroud) javascript ×4
asynchronous ×2
promise ×2
abstraction ×1
ajax ×1
chaining ×1
commonjs ×1
methods ×1
node.js ×1