Extension of functional parameters based on parameter type

Description

I have a function that passes the options parameter. This parameter can be an object , an array, or a string . Depending on what the parameter will determine what to do.


UPDATE: I forgot to mention that options should always end up as an object of the same structure (in other words, it should always set default values).

I only want to determine the default values ​​once, so using the if procedural instructions, as some of you have suggested, is not my preferred solution, but will resort to it if necessary.

I do not want to do this (if possible):

 function foo(options){ switch(typeof options){ case 'string': // do something etc break; // etc } } 

Example

If the parameter is an object, then expand it to set default values ​​as follows:

 function foo(options){ // Extend the options to apply default values var options = $.extend({ bar: 'none', baz: [] },options); } 

If the parameter is a string, set options.bar to a string and expand the default values ​​(something like this):

 function foo(options){ // Set the bar property to equal the supplied string var options = { bar: options }; // Extend the options to apply default values options = $.extend({ baz: [] },options); } 

If the parameter is an array, set options.baz equal to the array and extend the default values ​​(something like this):

 function foo(options){ // Set the baz property to equal the supplied array var options = { baz: options }; // Extend the options to apply default values options = $.extend({ bar: 'none' },options); } 

Question

Thus, I want to be able to specify the parameter in any format, and the function will build the same options object from what was provided. If no values ​​are specified, they resort to their default values.

Sorry, this is so incomprehensible, it is very difficult to explain.

Additional example

Another potential way I can demonstrate (jQuery) is to look at a function like animate () . Please note that you can either put:

 .animate( properties [, duration] [, easing] [, complete] ) 

or

 .animate( properties, options ) 

This additional example is not exactly what I hope to achieve, but it is on the right line.

+4
source share
2 answers

You can use various jQuery helper functions to determine the type of options :

 $.isPlainObject(options) $.isArray(options) 

and

 typeof options === "string" 

eg.

 function foo(par) { // default values var options = { bar: 'none', baz: [] }; if ($.isPlainObject(par)) { $.extend(options, par); } else if ($.isArray(par)) { options.baz = par; } else if (typeof options === "string") { options.bar = par; } ... } 

If you intend to change any of these values, use .slice() for copies of the array and the deep copy option to $.extend() so that the changes do not affect the provided object.

UPDATED RESPONSE

In this particular case, the answer is:

 function foo(parameter){ var options = { package: 'none', // Do not load any packages by default packageURL: false, // Do not retrieve the package details from a URL by default libraries: [] // Do not load any libraries by default }; // Determine the type of parameter supplied and // build the options accordingly if($.isArray(parameter)){ // Set libraries option parameter = { libraries: parameter }; }else if(typeof parameter === "string"){ // Set package option parameter = { package: parameter }; } // Extend the parameter object to include the default values $.extend(options, parameter); } 
+3
source

You can use typeof to determine the type of parameter supplied.
( typeof "abc" == "string"; typeof {a:1,b:2} == "object" )

Use this to determine part of the executable code (Switch / case or if / else)

+1
source

Source: https://habr.com/ru/post/1447474/


All Articles