Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同参数的函数重载JavaScript_Javascript_Function_Overloading - Fatal编程技术网

使用不同参数的函数重载JavaScript

使用不同参数的函数重载JavaScript,javascript,function,overloading,Javascript,Function,Overloading,我有一种叫做“leer”(英语学习)的方法,atm需要两个单词。1表示一个类别,1表示该类别。现在,我想添加一些功能,通过这些功能,我可以将一组单词添加到一个类别中 WoordenSchat.prototype.leer = function(categorie,naam){ if (!this.hasOwnProperty(categorie)){ this[categorie] = [] this[categorie].push(naam) }

我有一种叫做“leer”(英语学习)的方法,atm需要两个单词。1表示一个类别,1表示该类别。现在,我想添加一些功能,通过这些功能,我可以将一组单词添加到一个类别中

WoordenSchat.prototype.leer = function(categorie,naam){
    if (!this.hasOwnProperty(categorie)){
        this[categorie] = []
        this[categorie].push(naam)
    }
    else {
        this[categorie].push(naam)
    }
}
我可以通过计算通过typeOf在“naam”中接收到什么类型的变量来解决这个问题,然后相应地采取行动,但我觉得这会导致一段混乱的代码

我想做的是有两个功能:

  • leer(分类,naam)
  • leer(分类,[naam])
其中,具有名称数组(复数形式为naam(荷兰语))的人将调用for循环中的第一个名称

这在JavaScript中可能吗?因为据我所知,没有办法告诉Javascript方法:“你只接受这种类型的变量”


我知道在python中,您可以执行类似于def functionName(categie:str、naam:str)等的操作。

JavaScript不支持基于参数类型的函数重载。
但是,如所示,您可以修改函数以接受可变数量的参数:

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`.
    }
};
您可以根据需要在
categorie
之后添加任意数量的参数

此功能可以进一步简化:

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`.
    }
};

如果ES2015是一个选项:

WoordenSchat.prototype.leer = function(categorie, ...namen) {
  if (! this.hasOwnProperty(categorie)) {
    this[categorie] = [];
  }
  this[categorie].push(...namen);
}

您可以创建许多子函数,如
\u leerWithArray
\u leerWithString
,但最后,您仍然需要处理naam的类型并将aruguments传递给每个子函数。您可以通过循环调用
leer('x',a,b,c,d)
,使用任意数量的字符串。JS中没有内置函数重载。不管怎样,你都得检查一下arguments@fuyushimoya:问题是:对于分配,它必须是无缝的。在你的建议中,我必须用“leer”来将这两种类型分开,然后让子功能来完成这项工作。有点不是我想做的,但是如果我必须做的话,我会做的
typeof
是这里的方法。但是如果你想避免混乱的代码,不管怎样,还是用两种不同的方法。这不是作业要求的,但我还是喜欢答案。它还显示了一些我还不知道的东西:不管你在函数名中输入多少参数,它仍然可以保存很多参数。那么,赋值告诉你要做什么呢?不管怎样,我很乐意为您提供帮助。这是我们必须使用WoordenSchat和Madlibs重新创建的功能:
madlib.leren('naam',('Mercator','Caesar')
,下面的两个,以及
{}
都不是有效的JavaScript语法。这些是不可能重现的。不过,我会考虑添加一些东西来支持数组。@MrKickkiller:我在答案中添加了另一个选项,所以现在它也接受数组。
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]);
    }
};
myWoordenSchat.leer('programmeren', 'functies');
myWoordenSchat.leer('programmeren', 'functies', 'variabelen', 'constanten');
myWoordenSchat.leer('programmeren', ['functies', 'variabelen', 'constanten']);
WoordenSchat.prototype.leer = function(categorie, ...namen) {
  if (! this.hasOwnProperty(categorie)) {
    this[categorie] = [];
  }
  this[categorie].push(...namen);
}