Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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_Typescript - Fatal编程技术网

Javascript 实现函数重载

Javascript 实现函数重载,javascript,typescript,Javascript,Typescript,因为javascript不支持函数重载,所以typescript不支持它。但是,这是一个有效的接口声明: // function overloading only in the interface interface IFoo{ test(x:string); test(x:number); } var x:IFoo; x.test(1); x.test("asdf"); 但是我如何实现这个接口呢。Typescript不允许此代码: // function overload

因为javascript不支持函数重载,所以typescript不支持它。但是,这是一个有效的接口声明:

// function overloading only in the interface 
interface IFoo{
    test(x:string);
    test(x:number);
}

var x:IFoo;
x.test(1);
x.test("asdf");
但是我如何实现这个接口呢。Typescript不允许此代码:

// function overloading only in the interface 
interface IFoo{
    test(x:string);
    test(x:number);
}

class foo implements IFoo{
    test(x:string){

    }
    test(x:number){

    }
}

Typescript中的函数重载如下所示:

class foo implements IFoo {
    test(x: string);
    test(x: number);
    test(x: any) {
        if (typeof x === "string") {
            //string code
        } else {
            //number code
        }
    }
}

Typescript中的函数重载如下所示:

class foo implements IFoo {
    test(x: string);
    test(x: number);
    test(x: any) {
        if (typeof x === "string") {
            //string code
        } else {
            //number code
        }
    }
}

我想你已经在翻译器中发现了一个bug。将功能添加到接口的原因很清楚。这就是允许我们(通过接口)描述jquery如何工作的原因。但是我想了解的是如何在typescript中编写类似jquery的东西。我想说的是,您在翻译程序中发现了一个bug。将功能添加到接口的原因非常清楚。这就是允许我们(通过接口)描述jquery如何工作的原因。但我想了解的是如何在typescript中编写类似jquery的东西。