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).
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).