函数重载(function overloading),是 Ada、C++、C#、D、Java等编程语言中具备的一项特性,这项特性容许建立数项名称相同但输入输出类型或个数不一样的子程序,它能够简单地称为一个单独功能能够执行多项任务的能力。 维基百科-函数重载
函数重载是强类型语言的特性,虽然 js 是弱类型语言,咱们能够经过一些方法实现函数重载。html
班级中有不少学生,经过姓名要找到某个学生或多个学生时,同一个方法传入的参数个数的不一样去查找同窗。编程
const classRoom = { students: ['武大', '郎二', '张三', '张起灵', '李四', '王五'], } classRoom.find(); // ['武大', '郎二', '张三', '张起灵', '李四', '王五']; classRoom.find('张'); // ['张三', '张起灵']; classRoom.find('张', '三'); // ['张三'];
find()
方法不传入参数时,输出班级全部学生。find()
方法传一个参数(姓),输入班级相同姓的学生。find()
方法传两个个参数(姓,名),输入班级相同姓名的学生。咱们使用 arguments 和 switch 实现重载。闭包
classRoom.find = function() { switch(arguments.length) { case 0: return this.students; case 1: return this.students.filter(student => { let surname = arguments[0]; return ~student.indexOf(surname); }); case 2: return this.students.filter(student => { let fullName = Array.prototype.slice.call(arguments).join(''); return ~student.indexOf(fullName); }); } } console.log(classRoom.find()); // [ '武大', '郎二', '张三', '张起灵', '李四', '王五' ] console.log(classRoom.find('张')); // [ '张三', '张起灵' ] console.log(classRoom.find('三')); // [ '张三' ]
使用 arguments 和闭包。app
function addMethod(target, name, fn) { const old = target[name]; target[name] = function() { if (fn.length === arguments.length) { return fn.apply(this, arguments); } else if (typeof old === 'function') { return old.apply(this, arguments); } } } addMethod(classRoom, 'find', function() { return this.students; }); addMethod(classRoom, 'find', function(surname) { return this.students.filter(student => { return ~student.indexOf(surname); }); }); addMethod(classRoom, 'find', function(surname, personalName) { return this.students.filter(student => { return ~student.indexOf(surname + personalName); }); }); console.log(classRoom.find()); // [ '武大', '郎二', '张三', '张起灵', '李四', '王五' ] console.log(classRoom.find('张')); // [ '张三', '张起灵' ] console.log(classRoom.find('三')); // [ '张三' ]
调用addMethod
时会将匿名函数指向classRoom
中的find
,因为classRoom
是全局变量因此addMethod
执行完毕后其中的old
、fn
仍然在被引用,这里产生闭包。编程语言
因此在每次调用addMethod
时,都有一个执行环境保存着当时的old
以及fn
,因此在使用find
方法是能够找到fn
,实现重载。函数
须要注意的是:this
在 TypeScript 中加入了类型,它自带函数重载。ts函数prototype