函数重载
JavaScript并无供给函数重载
TypeScript供给的函数重载,仅仅用做提醒成果,完成借需脚动
函数重载简略点说即是,统一个函数的差异完成体式格局,依照传进的参数的范例或者者少度,决议终极挪用哪个完成
终极结果,TypeScript的范例校验也会变动
code
创立函数重载函数
依照参数的范例,挪用差异的完成,若何怎样不对于应的完成,则报错。
export function createOverload () {
const map: Map<string, Function> = new Map();
const overload = ( ...args: any[] ) => {
const key = args.map( it => typeof it ).join( " " );
const fn = map.get( key );
if ( !fn ) throw new Error( "No overload function matched" );
return fn( args );
};
overload.addImpl = function ( args: Array<"number" | "string" | "boolean" | "function" | "object" | "symbol" | "undefined" | "bigint">, fn: Function ) {
if ( typeof fn !== "function" ) throw new Error( "last argument must be a function" );
const key = args.join( " " );
map.set( key, fn );
};
return overload;
}
应用
const overload = createOverload()
overload.addImpl(["string","number"],()=>{
console.log("string number")
})
overload.addImpl(["number","string"],()=>{
console.log("number string")
})
overload("yang jun",18) // console.log("string number")
overload(18,"yang jun") // console.log("number string")
启拆一层,由于上述应用不代码提醒,离了代码提醒活没有上去了
建立形象类。
正在JS外建立形象类办法,正在construct外执止 if( new.target === Overload ) throw new Error("没有容许直截new")
不才述完成外,转为 es6 类完成,新删了重载函数的映照表,用于承继类的多个函数的重载。
export abstract class Overload {
private overloads = new Map<string, Function>();
protected addImpl ( name: string, args: Array<"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function">, fn: Function ) {
let overload = null;
if ( this.overloads.has( name ) ) {
overload = this.overloads.get( name );
} else {
overload = createOverload();
this.overloads.set( name, overload );
}
overload.addImpl( args, fn );
};
protected getOverload ( name: string ): Function | undefined {
if ( this.overloads.has( name ) ) return this.overloads.get( name );
throw new Error( "No overload matched" );
}
}
运用
极其惬意,不再用自身正在函数外写一串的 if 了
import { Overload } from "./utils";
class Test extends Overload {
constructor () {
super();
this.addImpl( "getInfo", [ "boolean", "number", "string" ], () => {
console.log( "boolean", "number", "string" );
} );
this.addImpl( "getInfo", [ "number", "string", "boolean" ], () => {
console.log( "number", "string", "boolean" );
} );
this.addImpl( "getInfo", [ "string", "number", "boolean" ], () => {
console.log( "string", "number", "boolean" );
} );
}
getInfo ( age: number, name: string, isHandsome: boolean ): Object;
getInfo ( isHandsome: boolean, age: number, name: string ): Object;
getInfo ( name: string, age: number, isHandsome: boolean ): Object;
getInfo (): Object {
const overload = this.getOverload( "getInfo" );
return overload( ...arguments );
}
getName ( name: string ) {
}
}
const test = new Test();
test.getInfo( 18, "yang jun", true ); // console.log( "number", "string", "boolean" );
test.getInfo( "yang jun", 18, true ); // console.log( "string", "number", "boolean" );
test.getInfo( true, 18, "yang jun" ); // console.log( "boolean", "number", "string" );
以上等于Typescript 完成函数重载的体式格局的具体形式,更多闭于Typescript函数重载的质料请存眷剧本之野其余相闭文章!
发表评论 取消回复