Reputation: 55353
Not sure if this is a dumb question. I've started playing with a few JavaScript frameworks and I always get confused when I see an argument that is not being used inside a function.
A Backbone example (the model arg):
var Todo = Backbone.Model.extend({
validate: function(attribs){
if(attribs.title === undefined){
return "Title can't be undefined";
}
},
initialize: function(){
console.log('This model has been initialized');
this.on('error', function(model, error){
console.log(error);
});
},
});
Express example (the req and next args):
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
So, I'm wondering, those values are just ignored? What would happen if you leave them out? And why they have to be included in the first place? (I'm used to the idea that if a function passes an argument is because it will be used in the function).
Upvotes: 0
Views: 124
Reputation: 221
You are correct that these arguments will be ignored if you do not use them. Also, they can be safely left out. But, if you only want to access the second argument you will need to have the first there as a placeholder.
As to why these arguments are there? It is important to understand that what you are passing in is a callback function. This callback is then called at a later point by the function that you passed it into. So it is the function provided by the framework that is calling your callback function and passing in these arguments. The Framework has no way of knowing what your specific use case is and has know way of knowing exactly what data your callback needs. Thus the authors of the framework pass in several arguments with enough data to cover as many use case as possible. While your callback function is not making use of these arguments other developers using these frameworks might be writing a callback that does.
Upvotes: 1
Reputation: 20155
You dont have to, unless you are using an argument that is after the an argument you dont use. And even then , you could use the argument object in a function to get the argument at the right index.
function myFunc(){
console.log(arguments);
}
myFunc(1,2,3,4,5,6); // logs 1,2,3,4,5,6
In a documentation , though , it is better to let the developper know what arguments are injected in the callback so the developper can have a clear view of the continuation configuration.
Upvotes: 1
Reputation: 1415
If those values are ignored, nothing would happen and it would work good as well. Whether those values are ignored or not, whether you use them or not, they are passed in actually.
Upvotes: 1
Reputation: 22692
In the case of initialize:
The variable model isn't used, but error is. Because error is the 2nd argument, model must be passed as a place holder.
The same thing is true in use:
The variable req isn't used, but it is required as a place holder so res can be used.
Why do the functions have parameters that don't appear to be used?
Maybe it's for backwards compatibility or for extensibility.
I assume these are both open source libraries (I've never used either).
Upvotes: 2