javascript 函数重载 overloading
函数重载
https://en.wikipedia.org/wiki/Function_overloading
In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations.
Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field.
#include <iostream> // volume of a cube
int volume(int s)
{
return s*s*s;
} // volume of a cylinder
double volume(double r, int h)
{
return 3.14*r*r*static_cast<double>(h);
} // volume of a cuboid
long volume(long l, int b, int h)
{
return l*b*h;
} int main()
{
std::cout << volume(10);
std::cout << volume(2.5, 8);
std::cout << volume(100, 75, 15); return 0;
}
javascript 函数重载方法
http://www.cnblogs.com/bluedream2009/archive/2011/01/05/1925963.html
javascript不支持函数重载,不能够定义同样的函数然后通过编译器去根据不同的参数执行不同的函数。
但是javascript却可以通过自身属性去模拟函数重载。
如上URL提供了一种 模拟 根据函数类型, 确定执行函数的方法:
var Calculate = FunctionH.overload({
'number,number': function () {
return arguments[] + arguments[];
},
'number,number,number': function () {
return arguments[] * arguments[] * arguments[];
}
}); alert(Calculate(,,));
overload函数实现:
var map = function (arr, callback, pThis) {
var len = arr.length;
var rlt = new Array(len);
for (var i = 0; i < len; i++) {
if (i in arr) rlt[i] = callback.call(pThis, arr[i], i, arr);
}
return rlt;
}
/**
* 函数参数重载方法 overload,对函数参数进行模式匹配。默认的dispatcher支持*和...以及?,"*"表示一个任意类型的参数,"..."表示多个任意类型的参数,"?"一般用在",?..."表示0个或任意多个参数
* @method overload
* @static
* @optional {dispatcher} 用来匹配参数负责派发的函数
* @param {func_maps} 根据匹配接受调用的函数列表
* @return {function} 已重载化的函数
*/
var FunctionH = {
overload: function (dispatcher, func_maps) {
if (!(dispatcher instanceof Function)) {
func_maps = dispatcher;
dispatcher = function (args) {
var ret = [];
return map(args, function (o) { return typeof o}).join();
}
} return function () {
var key = dispatcher([].slice.apply(arguments));
for (var i in func_maps) {
var pattern = new RegExp("^" + i.replace("*", "[^,]*").replace("...", ".*") + "$");
if (pattern.test(key)) {
return func_maps[i].apply(this, arguments);
}
}
}
}
};
成熟的Javascript库实现
https://github.com/JosephClay/overload-js
Provides tools to mimic function overloading that is present in most strictly-types languages. Prevents messy, long, if-statement, type-checking functions that are hard to read and maintain. Style and API inspired by Moreiki and Mongoose.
例子:
var hello = (function() { var secret = '!'; return overload()
.args().use(function() {
return secret;
})
.args(String).use(function(val) {
secret = val;
}); }()); hello('world'); // calls setter
hello(); // returns 'world'
hello(0); // throws a Type Error
Detectable types
null
undefined
Infinity
Date
NaN
Number
String
Object
Array
RegExp
Boolean
Function
Element // browser only
javascript 函数重载 overloading的更多相关文章
- JavaScript函数重载
译者按: jQuery之父John Resig巧妙地利用了闭包,实现了JavaScript函数重载. 原文: JavaScript Method Overloading 译者: Fundebug 为了 ...
- [转载]浅谈JavaScript函数重载
原文地址:浅谈JavaScript函数重载 作者:ChessZhang 上个星期四下午,接到了网易的视频面试(前端实习生第二轮技术面试).面了一个多小时,自我感觉面试得很糟糕的,因为问到的很多问题都 ...
- Javascript函数重载,存在呢—还是存在呢?
1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...
- JavaScript “函数重载”
函数重载(function overloading)必须依赖两件事情:判断传入参数数量的能力和判断传入参数类型的能力. JavaScript的每个函数都带有一个仅在这个函数范围内作用的变量argume ...
- 浅谈JavaScript函数重载
上个星期四下午,接到了网易的视频面试(前端实习生第二轮技术面试).面了一个多小时,自我感觉面试得很糟糕的,因为问到的很多问题都很难,根本回答不上来.不过那天晚上,还是很惊喜的接到了HR面电话.现在HR ...
- javascript arguments与javascript函数重载
1.所 有的函数都有属于自己的一个arguments对象,它包括了函所要调用的参数.他不是一个数组,如果用typeof arguments,返回的是’object’.虽然我们可以用调用数据的方法来调用 ...
- javascript 函数重载另一种实现办法
最近在读javascript忍者 感受下jquery作者 john Resig对于js的独到见解. 先上代码: function addMethod(object,name,fn){ var old ...
- 理解javascript函数的重载
javascript其实是不支持重载的,这篇文章会和java语言函数的重载对比来加深对javascript函数重载的理解. 以下我会假设读者不了解什么是重载所以会有一些很基础的概念 ...
- JavaScript模拟函数重载
JavaScript是没有函数重载的,但是我们可以通过其他方法来模拟函数重载,如下所示: <!DOCTYPE html> <html> <head> <met ...
随机推荐
- 利用ipython实现多线程
多线程来批量化处理数据的时候希望简单的应用,使用ipython会很简单 参考这里
- 已解决:Strict Standards: Only variables should be passed by reference in
今天安装ecshop的时候最上面出现了一个错误提示:Strict Standards: Only variables should be passed by reference in F:\www.x ...
- self.automaticallyAdjustsScrollViewInsets
导航视图内Push进来的以“TableView”(没有ScrollView截图,就将就一下)为主View的视图,本来我们的cell是放在(0,0)的位置上的,但是考虑到导航栏.状态栏会挡住后面的主视图 ...
- R中一切都是vector
0.可以说R语言中一切结构体的基础是vector! R中一切都是vector,vecotor的每个component必须类型一致(character,numeric,integer....)!vect ...
- FK JavaScript之:ArcGIS JavaScript API之地图动画
地图要素动画应用场景:动态显示地图上的要素的属性随着时间的改变而改变,并根据其属性的变化设置其渲染.比如:某水域项目中,随着时间的变化,动态展现水域的清淤进度 本文目的:对ArcGIS JavaScr ...
- antmate.css
本文各式各样的动画来源于http://daneden.github.io/animate.css/ ,若想看效果可复制运行下文,或到该网站自行查阅. <!DOCTYPE html> < ...
- maven模块
用maven无它,唯方便而. 模块依赖可以用来做一些公共模块,多个工程调用. 先子模块 install 或者package.在父模块install
- Zxing库
一.介绍 Zxing是一个开放的源码,用java实现的多种样式的1D/2D条码处理库,它包含了联系到其他语言的端口.Zxing可以实现手机的内置摄像头完成条码的扫描及解码.目前支持:UPC-A ,UP ...
- 配置Office 365单点登录过程中的一些注意事项
这些天一直在整O365单点登录的问题,其中涉及到了很多知识点,其中以ADFS,CA为主吧,IIS为辅.下面我就把这些天积累的一些经验写下来备用. 1. 申请证书不一定要通过“证书颁发机构Web注册”, ...
- IBM 3090 with Vector Facility
COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION