Javascript函数的参数arguments
arguments
Description
在所有的函数中有一个arguments对象,arguments对象指向函数的参数,arguments object is an Array-like object,除了length,不具备数组的其他属性。
访问: var a = arguments[0];
arguments可以改变: arguments[1] = 'new value';
arguments转换为一个真实的数组:
var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);
var args = Array.from(arguments);
var args = [...arguments];
Properties
arguments.callee
Reference to the currently executing function.
指向当前正在执行的函数的函数体。在匿名函数中很有用。
function fun(){
console.log(arguments.callee == fun);//true
console.log(arguments.callee);
/*
function fun(){
console.log(arguments.callee == fun);//true
console.log(arguments.callee);
}
*/
}
fun();
var global = this;
var sillyFunction = function(recursed) {
if (!recursed) { return arguments.callee(true); }
if (this !== global) {
console.log('This is: ' + this);
} else {
console.log('This is the global');
}
}
sillyFunction();//This is: [object Arguments]
使用arguments.callee在匿名递归函数中:
一个递归函数必须能够指向它自己,通过函数名可以指向自己,但是在匿名函数没有函数名,所以使用arguments.callee指向自己。
function create() {
return function(n) {
if (n <= 1)
return 1;
return n * arguments.callee(n - 1);
};
}
var result = create()(5);
console.log(result);// returns 120 (5 * 4 * 3 * 2 * 1)
arguments.caller
Reference to the function that invoked the currently executing function.这个属性已经被移出了,不再工作,但是仍然可以使用Function.caller.
function whoCalled() {
if (arguments.caller == null)
console.log('I was called from the global scope.');
else
console.log(arguments.caller + ' called me!');
}
whoCalled();//I was called from the global scope.
function whoCalled() {
if (whoCalled.caller == null)
console.log('I was called from the global scope.');
else
console.log(whoCalled.caller + ' called me!');
}
whoCalled();//I was called from the global scope.
function whoCalled() {
if (whoCalled.caller == null)
console.log('I was called from the global scope.');
else
console.log(whoCalled.caller + ' called me!');
}
function meCalled(){
whoCalled();
}
meCalled();
/*
function meCalled(){
whoCalled();
} called me!
*/
arguments.length
Reference to the number of arguments passed to the function.
arguments[@@iterator]
Returns a new Array Iterator object that contains the values for each index in the arguments.
Examples
function myConcat(separator) {
var args = Array.prototype.slice.call(arguments, 1);
return args.join(separator);
}
myConcat(', ', 'red', 'orange', 'blue');// returns "red, orange, blue"
function bar(a = 1) {
arguments[0] = 100;
return a;
}
bar(10); //
function zoo(a) {
arguments[0] = 100;
return a;
}
zoo(10); //
Javascript函数的参数arguments的更多相关文章
- JavaScript 之 function函数及参数arguments
JavaScript用function关键字声明函数,可以用return返回值,也可以没有返回值. 建议:要么统一有返回值,要么统一都没有返回值,这样调试代码方便. 函数定义格式: function ...
- JavaScript 函数 伪数组 arguments
一.函数 函数:函数就是将一些语言进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 1.将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动 2.简化编程,让变 ...
- Python中函数的参数-arguments
归纳起来,Python中函数的定义形式和调用形式主要有如下几种形式: # 函数的定义形式 def func(name) # 匹配positional参数或者keyword参数 def func(nam ...
- 浅析JavaScript函数的参数
ECAMScript函数不介意传递进来多少个参数,也不介意传递的参数的类型,即使定义的函数只接受两个参数,当调用该函数时没有传递参数,甚至传递了三个参数等等都无所谓,这是因为在ECAMScript中参 ...
- 【JavaScript】JavaScript函数的参数
要访问js函数中传入的所有参数,可以使用特殊的arguments变量.但是虽然可以像访问数组一样从arguments变量中读取参数,但arguments并非真正的数组.例如,arguments没有pu ...
- JavaScript函数中的arguments对象
ECMAScript标准中,每个函数都有一个特殊的内置对象arguments.arguments对象是一个类Array对象(object),用以保存函数接收到的实参副本. 一.内置特性 说它是一个内置 ...
- javascript函数嵌套时arguments的问题
疑问: var funtest = function () { var fun = function (val, val2) { alert(arguments.length); //此处答案? 有些 ...
- 深入理解javascript函数系列第二篇——函数参数
× 目录 [1]arguments [2]内部属性 [3]函数重载[4]参数传递 前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传 ...
- 理解JavaScript函数参数
前面的话 javascript函数的参数与大多数其他语言的函数的参数有所不同.函数不介意传递进来多少个参数,也不在乎传进来的参数是什么数据类型,甚至可以不传参数. arguments javascri ...
随机推荐
- jquery的find()
jQuery 遍历 - find() 方法 jQuery 遍历参考手册 实例 搜索所有段落中的后代 span 元素,并将其颜色设置为红色: $("p").find("sp ...
- 出租车Jt/T 905协议与部标1078协议融合的网约车视频监控平台
出租车jt/t 905协议,是jt/t 808协议的一个变种,设计者将部标808协议拿过来,并不是单纯的增加网约车相关的指令集,而且对原有的指令如定位0×0200指令也进行了修改,经过一通剧烈的修改, ...
- Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...
- LeetCode78:Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- git 撤销已经push到远端的代码
其实是没有直接让远端代码回复到某次的指令,实现撤销push的思路如下: 1.先让代码恢复到想要恢复的前一次提交记录 2.重新提交代码,覆盖端上的代码,就相当于撤销了push 的提交 实现方式如下: 1 ...
- 执行cp命令时提示cp: 略过目录
执行cp命令时提示cp: 略过目录 加入-r之后成功拷贝 在网上search了一下CP命令的用法: CP命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样, ...
- XML使用总结(一)
XML使用总结(一): XML是一种可拓展的标记语言,被设计用来描写叙述.存储及传递数据的语言体,而它的标签没有被提前定义,须要用户自行定义,是W3C推荐的数据存储和传递的标准标记语言. · ...
- 6.2.1-FactoryBeanRegistrySupport(未全)
FactoryBeanRegistrySupport 的关系图: 添加工厂方式创建类FactoryBean的支持
- table表格用tbody新属性获取DOM元素
// alert(oTab.getElementsByTagName("tbody")[0] // .getElementsByTagName('tr')[1] // .getEl ...
- 17.Django表单验证
Django提供了3中方式来验证表单 官网文档:https://docs.djangoproject.com/en/1.9/ref/validators 1.表单字段验证器 a.引入:from dja ...