This question has probably been asked before, but I don't really know the proper keywords to find a solution on Google, so all my researches returned 0 :) Or better, all my researches ended up with "optional parameters" but for functions.
Let's assume I have this class:
var Class = function (params) {
// I get the data somehow
params.name(data);
}
The problem is that not every time I instantiate new Class() I need also to set name() like:
new Class({
name: function (data) {
// Do something with the data
}
... other params ...
})
But, with the above syntax, if I don't declare name parameter as a function, an error params.name is not a function is thrown:
// Error
new Class({
... other params ...
})
My question is: is there a way to set params.name() as an optional parameter (in the object) that can be a function or completely not exist (e.g. null)? Something like saying: "when you instantiate the class:
- if you declare
name: function (data) {}=> then:params.name(data)is a function - if you skip
name=> it's anyway ok (no error is thrown)"