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 ...
随机推荐
- lua学习笔记(十二)
弱引用table lua使用自动内存管理机制,通过垃圾回收器来回收内存 垃圾回收器只能回收它认为是垃圾的内容,而不能回收用户认为是垃圾的内容 典型的例子栈,栈一般用一个数组和一 ...
- [译]GLUT教程 - 整合代码6
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VI 下面代码以窗体模式启动.你可以在 ...
- File类的源码学习
File类是Java中IO部分的一个类,用于表示文件或者目录的.关于File类有很多的常规操作这里就不介绍了,来看一下不常规的东西. File英文意思是文件,但是它也可以用来表示目录,文件的概念还是比 ...
- python reduce & map 习题
基于廖雪峰教程作业 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317 ...
- c语言三元组
// Triplet.cpp : 定义控制台应用程序的入口点.//#include "stdio.h"#include "stdlib.h"#define OK ...
- poj2115[扩展欧几里德]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22260 Accepted: 6125 Descr ...
- 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题
环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...
- socket java 实例
简单的java socket 示例 一.搭建服务器端 a).创建ServerSocket对象绑定监听端口. b).通过accept()方法监听客户端的请求. c).建立连接后,通过输入输出流读取客户端 ...
- TRansportation ANalysis and SIMulation System
https://www.fhwa.dot.gov/planning/tmip/transims/background.cfm?from=groupmessage
- (转)如何使VMware ip与本机ip处于同一网段
如何使VMware ip与本机ip处于同一网段 原创 2017年05月08日 17:28:56 1287 首先确认本机ip 可以看出一下信息: 本机ip: 192.168.1.162 网关:192. ...