admin管理员组文章数量:1363556
In Javascript, I have seen the callback function is passed as the last parameter I am curious why so? Is it a good practice or standard way?
For example:
var doSomething = function(fname, lname, callback){
console.log("Your name is :"+ fname +" "+ lname);
callback();
}
var callback = function(){
console.log("Your name is printed successfully."):
}
doSomething('Arpit', 'Meena', callback); // callback is last parameter here
I know we can pass it at any position and it works but I just want to know the reason behind this.
Thanks.
In Javascript, I have seen the callback function is passed as the last parameter I am curious why so? Is it a good practice or standard way?
For example:
var doSomething = function(fname, lname, callback){
console.log("Your name is :"+ fname +" "+ lname);
callback();
}
var callback = function(){
console.log("Your name is printed successfully."):
}
doSomething('Arpit', 'Meena', callback); // callback is last parameter here
I know we can pass it at any position and it works but I just want to know the reason behind this.
Thanks.
Share Improve this question edited Jan 29, 2018 at 11:06 Arpit Kumar asked Dec 30, 2016 at 10:28 Arpit KumarArpit Kumar 2,2495 gold badges31 silver badges56 bronze badges 3- What makes you think that it is passed last in most cases. Can you give some example – Panther Commented Dec 30, 2016 at 10:30
- May be its most plex or different type of argument , people tend to add in last – Panther Commented Dec 30, 2016 at 10:31
-
In asynchronous function and mostly in Node js we generally use
function(req, res, next){}
wherenext
is the callback function. – Arpit Kumar Commented Dec 30, 2016 at 10:34
2 Answers
Reset to default 10The reason I do this way and have seen others doing so is because of code readability, when you have a calback as the last argument, you can declare the callback inline without making the code unreadable. For example, if you pass the callback as the first parameter:
var doSomething = function(callback, fname, lname){
console.log("Your name is :"+ fname +" "+ lname);
callback();
}
You have to call it like:
doSomething(function callback(){
console.log('foo');
}, 'Arpit', 'Meena');
However, if you use it as the last argument, things are kept much more clear on the function call:
var doSomething = function(fname, lname, callback){
console.log("Your name is :"+ fname +" "+ lname);
callback();
}
doSomething('Arpit', 'Meena', function callback(){
console.log('foo');
});
It is standard JavaScript convention. Part of the reason it has bee standard practice is that it makes things more readable. It's like defining properties at the top of a class - you don't have to but it's standard practice.
Suggested reading: http://technosophos./2012/08/22/javascript-callbacks-function-last%E2%80%A6-or-lost.html
http://hancic.info/callback-parameter-should-always-be-last
本文标签: functionWhy callback is always passed as last parameter JavaScriptStack Overflow
版权声明:本文标题:function - Why callback is always passed as last parameter JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743754350a2533232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论