JavaScript method overload
JavaScript doesn't support what you would call in other languages method overloading, but there are multiple workarounds, like using the arguments
object, to check with how many arguments a function has been invoked:
function f1(a, b, c) {
if (arguments.length == 2) {
// f1 called with two arguments
} else if (arguments.length == 3) {
// f1 called with three arguments
}
}
Additionally you could type-check your arguments, for Number and String primitives is safe to use the typeof
operator:
function f1(a, b, c) {
if (typeof a == 'number' && typeof b == 'number') {
// a and b are numbers
} else if (typeof a == 'string' && typeof b == 'number' &&
typeof c == 'number') {
// a is a string, b and c are numbers
}
}
And there are much more sophisticated techniques like the one in the following article, that takes advantage of some JavaScript language features like closures, function application, etc, to mimic method overloading:
JavaScript method overload的更多相关文章
- javascript method.
//**************************************************************** //* 名 称:DataLength //* 功 能:计算数据的长 ...
- javascript 函数重载 overloading
函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function over ...
- 转---- javascript prototype介绍的文章
JavaScript是基于对象的,任何元素都可以看成对象.然而,类型和对象是不同的.本文中,我们除了讨论类型和对象的一些特点之外,更重要的是研究如何写出好的并且利于重用的类型.毕竟,JavaScrip ...
- JavaScript:最烂与最火
============================================================================== 一.世无英雄,遂使竖子成名 1 Web客户 ...
- javascript函数没有重载测试
今天继续学习javascript系列教程,虽然是基础,但我觉得还是有必要用心来学习的,不要怕难,不用怕忘记,不要怕学不会.哪个高手不是从零开始的,我要坚定自己的学习信心,并且认真的走下去.虽然路途艰辛 ...
- javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等
使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...
- Device.js——检测设备平台、操作系统的Javascript 库
http://segmentfault.com/a/1190000000373735 Device.js 是一个可以让你检测设备的平台,操作系统和方向 JavaScript 库,它会自动在 <h ...
- 3 Ways to Preload Images with CSS, JavaScript, or Ajax---reference
Preloading images is a great way to improve the user experience. When images are preloaded in the br ...
- JavaScript函数重载
译者按: jQuery之父John Resig巧妙地利用了闭包,实现了JavaScript函数重载. 原文: JavaScript Method Overloading 译者: Fundebug 为了 ...
随机推荐
- 剑指Offer编程题(Java实现)——链表中环的入口结点
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路一 迭代遍历链表,利用HashSet将每个结点添加到哈希表中,如果添加失败(重复遍历了这个结点即遇到环),输出 ...
- Zend_Cache的使用
一.Zend_Cache快速浏览 Zend_Cache 提供了一个缓存任何数据的一般方法. 在Zend Framework中缓存由前端操作,同时通过后端适配器(File, Sqlite, Memcac ...
- 关于Maven的安装和配置
1.Maven的介绍 1.Maven是一个项目管理工具(项目对象模型POM) 2.Maven可以管理项目中的jar包依赖 3.Maven的中央仓库地址 http://mvnrepository.com ...
- TestCase维护和思维导图
在软件整个生命周期中,测试应该尽早地开始,因为测试对象不只是程序,还有文档和数据,所以针对需求分析说明书.概要设计和详细设计说明书,测试如何快速理解项目需求,进行下一步的工作呢? 本人觉得,如果只是看 ...
- 阿里云服务器重启出现An error occurred 如何处理
最近网站重启阿里云服务后,出现 An error occurred, An error occurred. Sorry, the page you are looking for is current ...
- 制作的第一个java小游戏
package java1; import java.awt.*; public class java1 extends Frame { //球桌和桌球图片 Image ball = Toolkit. ...
- 2019 安洵杯 Re 部分WP
0x01.EasyEncryption 测试文件:https://www.lanzous.com/i7soysb 1.IDA打开 int sub_416560() { int v0; // eax i ...
- PHPstorm快捷键介绍总结
如下所示: Eclipse快捷键 Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt ...
- Django框架简易图
- Nginx 配置状态信息虚拟主机
可以在浏览器中查看并发数量 [root@Liangenyu conf]# vim nginx.conf server { listen 80; server_name status.etiantian ...