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 ...
随机推荐
- js中有特殊字符的编码格式
在get和post方法中,如果传入的参数值有特殊字符,如:“&”,在get中的url需要拼接,可以使用encodeURICompontent来编码来转化 回调就是在上面传递实际参数,传递给aj ...
- JAVA问题之泛型数组
java中类似下面的代码编译器是会报错的: LinkedList<LinkedList<String>>[] li=new LinkedList<LinkedList ...
- DJI SDK iOS 开发之中的一个:前言
写这个开发教程之前,还是先说点什么. 首先要声明的是我并非DJI的员工.仅仅是DJI 飞行器的爱好者. 在DJI的phantom出来之后.我就一直期待着能够推出SDK.之前最早是Parrot的AR D ...
- 同样的代码在java和c++中结果不同
#include <iostream> using namespace std; /* run this program using the console pauser or add y ...
- python接口自动化(四十二)- 项目结构设计之大结局(超详解)
简介 这一篇主要是将前边的所有知识做一个整合,把各种各样的砖块---模块(post请求,get请求,logging,参数关联,接口封装等等)垒起来,搭建一个房子.并且有很多小伙伴对于接口项目测试的框架 ...
- Spring 和 filter
标题是 spring和filter,但是这里却是说的spring MVC 项目中需要用到filter,filter中需要用到spring实例化的bean,于是为了简化就形成spring和filter了 ...
- Spring 定时作业
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我 ...
- 九度OJ 1335:闯迷宫 (BFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1782 解决:483 题目描述: sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫. sun的室友在帮电脑节设计 ...
- 九度OJ 1260:珍珠项链 (字符串处理、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:101 解决:27 题目描述: 假设有一条珍珠项链,有很多珍珠,r代表红色, b代表蓝色, w代表白色. 假设你在某一处剪开之后,你会沿着顺时 ...
- [IOS]从零开始搭建基于Xcode7的IOS开发环境和免开发者帐号真机调试运行第一个IOS程序HelloWorld
首先这篇文章比较长,若想了解Xcode7的免开发者帐号真机调试运行IOS程序的话,直接转到第五部分. 转载请注明原文地址:http://www.cnblogs.com/litou/p/4843772. ...