argument
js中arguments的用法
了解arguments这个对象之前先来认识一下javascript的一些功能:
其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载。Javascrip中每个函数都会有一个Arguments对象实例arguments,它引用着函数的实参,可以用数组下标的方式"[]"引用arguments的元素。arguments.length为函数实参个数,arguments.callee引用函数自身。
arguments他的特性和使用方法
特性:
1.arguments对象和Function是分不开的。
2.因为arguments这个对象不能显式创建。
3.arguments对象只有函数开始时才可用。
使用方法:
虽然arguments对象并不是一个数组,但是访问单个参数的方式与访问数组元素的方式相同
例如:
arguments[0],arguments[1],。。。arguments[n];
在js中 不需要明确指出参数名,就能访问它们,例如:

function test() {
var s = "";
for (var i = 0; i < arguments.length; i++) {
alert(arguments[i]);
s += arguments[i] + ",";
}
return s;
}
test("name", "age");
输出结果:
name,age

我们知道每一个对象都有自己的属性,arguments对象也不例外,首先arguments的访问犹如Array对象一样,
用0到arguments.length-1来枚举每一个元素。下面我们来看看callee属性,返回正被执行的 Function 对象,
也就是所指定的 Function 对象的正文。callee 属性是 arguments 对象的一个成员,仅当相关函数正在执行时才可用。
callee 属性的初始值就是正被执行的 Function 对象。实现匿名的递归函数。代码如下:

var sum = function (n) {
if (1 == n) {
return 1;
} else {
return n + arguments.callee(n - 1);
}
}
alert(sum(6));
输出结果:21

通俗一点就是,arguments此对象大多用来针对同个方法多处调用并且传递参数个数不一样时进行使用。根据arguments的索引来判断执行的方法。
知识扩展:
当使用arguments进行函数传递时,有一些需要注意的点。例子如下:

var length = 10;
function fn() {
console.log(this.length);
} var obj = {
method: function(fn) {
fn();
arguments[0]();
}
}; obj.method(fn, 1); 输出:10,2

这里有2个需要注意的点。fn函数里面的this的指向:
1.第一个值为10,执行的是method里面的第一行"fn()",这里this指向的window。所以输出的值为最外层定义的length。
2.第二个值为2,执行的是method里面的第二行"arguments[0]()"(arguments[0]() => fn() ),这里this执行的是arguments这个对象,所以输出值为arguments的长度
arguments 的用法和特性基本就是这么多了。可能callee属性用到的比较少。但是如果自己封装或者写一些js的时候 除了callee的东西基本都会用到。有不对的地方希望朋友们多多支出。大家共同进步。
argument的更多相关文章
- tomcat启动时候报错Can't convert argument: null
一.启动报错: 为了避免导入的项目重名,我先修改了前一个项目的名称. 重新启动该项目至tomcat,报错:java.lang.IllegalArgumentException: Cant conver ...
- file_put_contents 错误:failed to open stream: Invalid argument 一种原因
今天在测试nilcms系统的时候,出现了一个报错,导致缓存无法更新: file_put_contents(C:\UPUPW_AP5.4\vhosts\d.tv\NilCMS_APP\include_r ...
- fdisk添加分区引起的Linux Error: 22: Invalid argument
在Linux服务器(虚拟机)上使用fdisk添加分区.格式化分区后,遇到了Linux Error: 22: Invalid argument错误,操作步骤如下所示 [root@oracle-serve ...
- -bash: ulimit: pipe size: cannot modify limit: Invalid argument
从root账号切换到oracle账号时,出现了"-bash: ulimit: pipe size: cannot modify limit: Invalid argument"提示 ...
- diff/merge configuration in Team Foundation - common Command and Argument values - MSDN Blogs
One of the extensibility points we have in Team Foundation V1 is that you can configure any other di ...
- -bash: /bin/rm: Argument list too long
使用rm * -f删除缓存目录文件时,报如下错误 -bash: /bin/rm: Argument list too long 提示文件数目太多. 解决的办法是使用如下命令: ls | xargs - ...
- 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument
这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...
- senlin __init__() got an unexpected keyword argument 'additional_headers'
从senlin源码重新编译更新了服务,然后执行 senlin的 cli就遇到了错误: __init__() got an unexpected keyword argument 'additional ...
- could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...
- -[NSBundle initWithURL:]: nil URL argument'
今天早上同事突然跟我说趣拍的SDK不能用了,一调用就crash,我一听就纳了闷了,原来好好的啊. 然后就开始查呗,马上就要上线了,不搞好,老大会不会杀了我... 搞个全局断点,就停在了一堆我看不懂的界 ...
随机推荐
- c++ STL vector初步学习
/*vector(向量):是一种顺序容器,,动态数组,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷, ...
- Progressbar 实例
Progressbar 实例原创侠之大者为国为民 最后发布于2015-10-28 15:22:34 阅读数 5394 收藏展开Progressbar - orient 配置进度条的方向:"h ...
- C++中局部变量的返回
在写 “根据中序和后序遍历顺序,构建树的问题” 时,原本这只是一个非常简单的问题,但是突然发现一直有错误.代码如下: node* get_root(int x1, int x2, int y1, in ...
- [0CTF 2016]piapiapia{PHP反序列化漏洞(PHP对象注入)}
先上学习链接: https://www.freebuf.com/column/202607.html https://www.cnblogs.com/ichunqiu/p/10484832.html ...
- Python模块/包/库安装几种方法(转载)
一.方法1: 单文件模块直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包(压缩文件zip或tar.gz),进行解压,CMD->cd进入模 ...
- AcWing 1012. 友好城市
#include<iostream> #include<algorithm> using namespace std ; typedef pair<int,int> ...
- AcWing 894. 拆分-Nim游戏
#include <cstring> #include <iostream> #include <algorithm> #include <unordered ...
- 记录 shell学习过程(5)continue break
1.continue ;i<;i++)) do ];then continue fi echo $i done # ./continue.sh12346789 2.break ;i<;i+ ...
- linux异常 - 弹出界面 eth0:设备eth0似乎不存在
问题描述: 用VMware vSphere Client复制虚拟机之后,出现这个问题 解决方法: service network stop service NetworkManager restart
- Git 添加远程github仓库的时候提示错误:fatal: remote origin already exists.
1.先删除远程 Git 仓库 $ git remote rm origin 2.再添加远程 Git 仓库 $ git remote add origin git@github.com:wsydxian ...