javascript - Is it a good practice to allow a string or an array as argument to a function? -


here's example of i'm talking about:

with grunt, have function:

grunt.file.match([options, ] patterns, filepaths) 

in doc, say:

both patterns , filepaths argument can single string or array of strings.

so, practice allow different types same parameter?

official doc

in short, yes. in other languages can overload method different parameters. in javascript, not have luxury, have weak-type declarations. javascript not care pass, job make sure function accepts possible arguments.

edit @jwatts1980 suggested, function should flexible enough handle potential arguments. if have parameter called name, should expect null, , chance might have receive array of names, in case might choose iterate on names (name) , whatever 1 name.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -