I have this method called 'leer' (Learn in English) that atm takes in 2 words. 1 for a category and 1 to put in that category. Now I would like to add functionality whereby I can add a bunch of words to one category.
WoordenSchat.prototype.leer = function(categorie,naam){
if (!this.hasOwnProperty(categorie)){
this[categorie] = []
this[categorie].push(naam)
}
else {
this[categorie].push(naam)
}
}
I could solve this by figuring out what sort of variable I receive in 'naam' via typeOf and then act accordingly, but I feel like this would result in a messy piece of code.
What I would like to do is have 2 functions:
- leer (categorie,naam)
- leer (categorie, [naam])
where by the one with an array of names (naam (Dutch) in plural) would call the first one in a for loop.
Is this possible in JavaScript? Because as far as I know there is no way of telling a Javascript method: "You take in this type of variable and this type only"
I know in python you could do things like this def functionName (categorie:str, naam: str) etc.
_leerWithArray,_leerWithString, but at last, you still need handle the types of naam and pass the aruguments to each subfunction.arguments.lengthand callleer('x', a, b, c, d)with as many strings as you like.typeofis the way to go here. But if you want to avoid messy code, go with two different methods anyway.