[英]Function overloading with different arguments JavaScript
I have this method called 'leer' (Learn in English) that atm takes in 2 words. 我有一种称为“ leer”(英语学习)的方法,atm用2个单词表示。 1 for a category and 1 to put in that category.
1表示一个类别,1表示该类别。 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. 我可以通过找出我通过typeOf在“ naam”中接收到的哪种变量然后采取相应的措施来解决此问题,但是我觉得这会导致代码混乱。
What I would like to do is have 2 functions: 我想做的是有2个功能:
where by the one with an array of names (naam (Dutch) in plural) would call the first one in a for loop. 其中带有名称数组的名称(naam(荷兰语)为复数)将在for循环中调用第一个。
Is this possible in JavaScript? 这在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"
因为据我所知,没有办法告诉Javascript方法:“您只能接受这种类型的变量,而只能接受这种类型”
I know in python you could do things like this def functionName (categorie:str, naam: str) etc. 我知道在python中你可以做这样的事情def functionName(categorie:str,naam:str)等。
JavaScript doesn't support function overloading based on parameter types. JavaScript不支持基于参数类型的函数重载。
However, as @AlexK suggested , you can modify the function to accept a variable amount of parameters: 但是,正如@AlexK 建议的那样 ,您可以修改函数以接受可变数量的参数:
WoordenSchat.prototype.leer = function(categorie){
if (!this.hasOwnProperty(categorie)){ // We only need to do this check once.
this[categorie] = [];
}
for(var i = 1; i < arguments.length; i++){
this[categorie].push(arguments[i]); // `arguments[i]` is the current `naam`.
}
};
This starts looking for parameters at arguments[1]
, since arguments[0]
is categorie
. 因为
arguments[0]
是categorie
,所以这开始在arguments[1]
处寻找参数。 Then, every single naam
passed to leer
will be pushed onto the array. 然后,传递给
leer
每一个naam
将被推到阵列上。
You can call the function like this: 您可以这样调用函数:
myWoordenSchat.leer('programmeren', 'functies', 'variabelen', 'constanten');
You can add as many or as few parameters after categorie
as you wish. 您可以根据需要在
categorie
后添加任意数量的参数。
This function can be simplified a little bit more: 此功能可以进一步简化:
WoordenSchat.prototype.leer = function(categorie){
this[categorie] = this[categorie] || []; // Make sure `this[categorie]` exists
for(var i = 1; i < arguments.length; i++){
this[categorie].push(arguments[i]); // `arguments[i]` is the current `naam`.
}
};
Now, you could modify the function to accept stings or an array, instead: 现在,您可以修改函数以接受字符串或数组,而不是:
WoordenSchat.prototype.leer = function(categorie, namen){
this[categorie] = this[categorie] || [];
if(typeof namen === 'string'){ // The first parameter after `categorie` is a string
namen = arguments.splice(1); // So, get all parameters after `categorie` as array
}
for(var i = 0; i < namen.length; i++){
this[categorie].push(namen[i]);
}
};
So, if the first parameter after categorie
is a string, iterate over all parameters, otherwise, iterate over the namen
array. 因此,如果
categorie
之后的第一个参数是字符串,则迭代所有参数,否则,迭代namen
数组。
You should be able to call the function like this: 您应该能够像这样调用该函数:
myWoordenSchat.leer('programmeren', 'functies');
myWoordenSchat.leer('programmeren', 'functies', 'variabelen', 'constanten');
myWoordenSchat.leer('programmeren', ['functies', 'variabelen', 'constanten']);
If ES2015 is an option: 如果可以选择ES2015:
WoordenSchat.prototype.leer = function(categorie, ...namen) {
if (! this.hasOwnProperty(categorie)) {
this[categorie] = [];
}
this[categorie].push(...namen);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.