Javascript object options: function or null
Javascript object options: function or null
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.
0
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()
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:
params.name is not a function
// 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:
params.name()
null
name: function (data)
params.name(data)
name
3 Answers
3
In ES6, You can use default function parameter option, like
var Class = function(params =
name: (x) => x
)
// I get the data somehow
data='value';
params.name(data);
Class();
Class(name: x => console.log(x));
Hope this will help!
EDIT
Yes @brigo, you are right, sorry I overlooked that point. We are actually looking for nested default rather. A possible solution could be to destructure the object inside the function like
var Class = function(params = )
// I get the data somehow
data = 'value';
withDefaultParams =
name: (x) => x,
...params
withDefaultParams.name(data);
Class();
Class(
name: x => console.log('non-default')
);
Class(
others: x => console.log('non-default')
);
name
params
undefined
Class()
params.name is not a function
@Brigo, yes you are right, I have overlooked that flow. I have updated the answer, may not be the best but a possible solution.
– Prasun
Sep 9 '18 at 14:28
yes, you could do something like
if (typeof param.name === 'function')
param.name(data);
else
// dont do anything or do something different
I was hoping for a shortener, cleaner and integrated solution (as @Pransun Pal's one could have been) but, at the moment, as you suggested this remains the only one I know
– Brigo
Sep 9 '18 at 13:56
@Brigo: I posted a different approach below. Maybe that's whar you are looking for?
– iPirat
Sep 9 '18 at 14:06
If using jQuery is an option, you could use its extend
method (API docs) like in the following example:
extend
var Class = function(params)
var _defaults =
name: function(),
some: 'other stuff'
;
params = jQuery.extend(_defaults,params);
// I get the data somehow
data='value';
params.name(data);
Class();
Class(foo:'bar');
Class(name: console.log );
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This would be the best solution if only the functionalities of default parameters would work also "against" the object's items. Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. But
name
, in this case, is not directly a parameter (params
is the parameter) so, if not provided (= it'sundefined
), callingClass()
will throw the same errorparams.name is not a function
.– Brigo
Sep 9 '18 at 13:49