📜  JavaScript 中的函数重载

📅  最后修改于: 2022-05-13 01:56:51.930000             🧑  作者: Mango

JavaScript 中的函数重载

与其他编程语言不同,JavaScript不支持函数重载
这是一个小代码,它表明 JavaScript 不支持函数重载。

Javascript
function foo(arg1) {
    console.log(arg1);
}
 
/* The above function will be
   overwritten by the function
   below, and the below function
   will be executed for any number
   and any type of arguments */
function foo(arg1, arg2) {
    console.log(arg1, arg2);
}
 
// Driver code
foo("Geeks")


Javascript
// Creating a class  "foo"
class foo {
 
    // Creating an overloadable method/function.
    overloadableFunction() {
 
        // Define three overloaded functions
        var function1 = function (arg1) {
            console.log("Function1 called with"
                    + " arguments : " + arg1);
            return arg1;
        };
 
        var function2 = function (arg1, arg2) {
            console.log("Function2 called with"
                    + " arguments : " + arg1
                    + " and " + arg2);
            return arg1 + arg2;
        };
 
        var function3 = function (arg1) {
            var concatenated__arguments = " ", temp = " "
 
            // Concatenating all the arguments
            // and storing them into a string
            for (var i = 0; i < arg1.length; i++) {
                concatenated__arguments =
                    concatenated__arguments + arg1[i]
            }
 
            /* Just ignore this loop and temp variable,
               we are using this loop to concatenate
               arguments with a space between them */
            for (var i = 0; i < arg1.length; i++) {
                temp = temp + " " + arg1[i]
            }
 
            console.log("Function3 called with this"
                + " array as an argument : [" + temp + "]");
            console.log("Output of log is : ")
 
            // Returns concatenated argument string
            return concatenated__arguments;
        };
 
        /* Here with the help of the length of the
           arguments and the type of the argument
           passed ( in this case an Array ) we
           determine which function to be executed */
        if (arguments.length === 1
                && Array.isArray(arguments[0])) {
            return function3(arguments[0]);
        } else if (arguments.length === 2) {
            return function2(arguments[0], arguments[1]);
        } else if (arguments.length === 1
                && !Array.isArray(arguments[0])) {
            return function1(arguments[0]);
        }
    }
}
 
// Driver Code
 
// Instantiate an object of the "foo" class
var object = new foo();
 
// Call the overloaded functions using the
// function overloadableFunction(...)
// We are passing 1 argument so executes function1
console.log(object.overloadableFunction("Geeks"));
 
// We are passing two arguments so executes function2
console.log(object.overloadableFunction("Geeks", "for"));
 
// We are passing an array so executes function3
console.log(object.overloadableFunction(
                ["Geeks", "for", "Geeks"]));


输出:

Geeks undefined 

输出中“未定义”的原因是:在 JavaScript 中,如果定义了两个同名的函数,那么最后定义的函数将覆盖前一个函数。
所以在这种情况下 foo(arg1) 被 foo(arg1,arg2) 覆盖,但我们只传递了一个
函数的参数(“极客”)。这意味着第二个参数是未定义的,所以当我们试图打印第二个参数时,它被打印为“未定义”。

我们已经看到 JavaScript 不支持函数Overloading,但是我们可以自己实现函数Overloading,当涉及到更多数量和更多类型的参数时,这非常复杂。以下代码将帮助您了解如何在 JavaScript 中实现函数重载。

Javascript

// Creating a class  "foo"
class foo {
 
    // Creating an overloadable method/function.
    overloadableFunction() {
 
        // Define three overloaded functions
        var function1 = function (arg1) {
            console.log("Function1 called with"
                    + " arguments : " + arg1);
            return arg1;
        };
 
        var function2 = function (arg1, arg2) {
            console.log("Function2 called with"
                    + " arguments : " + arg1
                    + " and " + arg2);
            return arg1 + arg2;
        };
 
        var function3 = function (arg1) {
            var concatenated__arguments = " ", temp = " "
 
            // Concatenating all the arguments
            // and storing them into a string
            for (var i = 0; i < arg1.length; i++) {
                concatenated__arguments =
                    concatenated__arguments + arg1[i]
            }
 
            /* Just ignore this loop and temp variable,
               we are using this loop to concatenate
               arguments with a space between them */
            for (var i = 0; i < arg1.length; i++) {
                temp = temp + " " + arg1[i]
            }
 
            console.log("Function3 called with this"
                + " array as an argument : [" + temp + "]");
            console.log("Output of log is : ")
 
            // Returns concatenated argument string
            return concatenated__arguments;
        };
 
        /* Here with the help of the length of the
           arguments and the type of the argument
           passed ( in this case an Array ) we
           determine which function to be executed */
        if (arguments.length === 1
                && Array.isArray(arguments[0])) {
            return function3(arguments[0]);
        } else if (arguments.length === 2) {
            return function2(arguments[0], arguments[1]);
        } else if (arguments.length === 1
                && !Array.isArray(arguments[0])) {
            return function1(arguments[0]);
        }
    }
}
 
// Driver Code
 
// Instantiate an object of the "foo" class
var object = new foo();
 
// Call the overloaded functions using the
// function overloadableFunction(...)
// We are passing 1 argument so executes function1
console.log(object.overloadableFunction("Geeks"));
 
// We are passing two arguments so executes function2
console.log(object.overloadableFunction("Geeks", "for"));
 
// We are passing an array so executes function3
console.log(object.overloadableFunction(
                ["Geeks", "for", "Geeks"])); 

输出:

Function1 called with arguments : Geeks
Geeks

Function2 called with arguments : Geeks and for
Geeksfor

Function3 called with this array as an argument : [ Geeks for Geeks]
GeeksforGeeks

Output of log is :
GeeksforGeeks

解释:
在上面的程序中,当不同数量的参数传递给同一个函数时,根据参数的数量和类型,参数将传递给各自的函数。
在这种情况下,我们使用了三个不同的函数( function1、function2、function3 )来进行函数重载。