arguments
对象
JavaScript 中每个函数内都能访问一个特别变量 arguments
。这个变量维护着所有传递到这个函数中的参数列表。
注意: 由于 arguments
已经被定义为函数内的一个变量。
因此通过 var
关键字定义 arguments
或者将 arguments
声明为一个形式参数,
都将导致原生的 arguments
不会被创建。
arguments
变量不是一个数组(Array
)。
尽管在语法上它有数组相关的属性 length
,但它不从 Array.prototype
继承,实际上它是一个对象(Object
)。
因此,无法对 arguments
变量使用标准的数组方法,比如 push
, pop
或者 slice
。
虽然使用 for
循环遍历也是可以的,但是为了更好的使用数组方法,最好把它转化为一个真正的数组。
转化为数组
下面的代码将会创建一个新的数组,包含所有 arguments
对象中的元素。
Array.prototype.slice.call(arguments);
这个转化比较慢,在性能不好的代码中不推荐这种做法。
传递参数
下面将参数从一个函数传递到另一个函数,是推荐的做法。
function foo() {
bar.apply(null, arguments);
}
function bar(a, b, c) {
// do stuff here
}
另一个技巧是同时使用 call
和 apply
,创建一个快速的解绑定包装器。
function Foo() {}
Foo.prototype.method = function(a, b, c) {
console.log(this, a, b, c);
};
// Create an unbound version of "method"
// 输入参数为: this, arg1, arg2...argN
Foo.method = function() {
// 结果: Foo.prototype.method.call(this, arg1, arg2... argN)
Function.call.apply(Foo.prototype.method, arguments);
};
译者注:上面的 Foo.method
函数和下面代码的效果是一样的:
Foo.method = function() {
var args = Array.prototype.slice.call(arguments);
Foo.prototype.method.apply(args[0], args.slice(1));
};
自动更新
arguments
对象为其内部属性以及函数形式参数创建 getter 和 setter 方法。
因此,改变形参的值会影响到 arguments
对象的值,反之亦然。
function foo(a, b, c) {
arguments[0] = 2;
a; // 2
b = 4;
arguments[1]; // 4
var d = c;
d = 9;
c; // 3
}
foo(1, 2, 3);
性能真相
arguments
对象总会被创建,除了两个特殊情况 - 作为局部变量声明和作为形式参数。
而不管它是否有被使用。
arguments
的 getters 和 setters 方法总会被创佳;因此使用 arguments
对性能不会有什么影响。
除非是需要对 arguments
对象的属性进行多次访问。
ES5 提示: 这些 getters 和 setters 在严格模式下(strict mode)不会被创建。
译者注:在 MDC 中对 strict mode
模式下 arguments
的描述有助于我们的理解,请看下面代码:
// 阐述在 ES5 的严格模式下 `arguments` 的特性
function f(a) {
"use strict";
a = 42;
return [a, arguments[0]];
}
var pair = f(17);
assert(pair[0] === 42);
assert(pair[1] === 17);
然而,的确有一种情况会显著的影响现代 JavaScript 引擎的性能。这就是使用arguments.callee
。
function foo() {
arguments.callee; // do something with this function object
arguments.callee.caller; // and the calling function object
}
function bigLoop() {
for(var i = 0; i < 100000; i++) {
foo(); // Would normally be inlined...
}
}
上面代码中,foo
不再是一个单纯的内联函数 inlining(译者注:这里指的是解析器可以做内联处理),
因为它需要知道它自己和它的调用者。
这不仅抵消了内联函数带来的性能提升,而且破坏了封装,因此现在函数可能要依赖于特定的上下文。
因此强烈建议大家不要使用 arguments.callee
和它的属性。
ES5 提示: 在严格模式下,arguments.callee
会报错 TypeError
,因为它已经被废除了。
相关推荐
前端开源库-arguments-extended参数扩展,用于处理参数对象的实用程序
资源来自pypi官网。 资源全名:flake8-force-keyword-arguments-1.0.2.tar.gz
移位参数 移动 javascript 函数参数以更好地与不同框架集成 npm install shift-arguments用法删除参数 var shiftArguments = require('shift-arguments');function testFunc (result) {}function doSthAsync ...
validate-arguments-js 验证参数,声明性的。安装 npm install validate-arguments 代码本身取决于lodash ,并且可以使用require.js在浏览器中require.js 。文献资料该模块lodash一些验证方法,但是通过为您提供了...
在函数定义时没有形参名称时,向函数传递参数的使用方法。 实例演示了arguments对象的使用方法。
ECMAScript中的函数并不介意传递的参数有多少,也不介意是什么类型。由于JavaScript允许函数有不定数目的参数,所以我们需要一种机制,可以在 函数体内 ...这篇文章将详细介绍Javascript中的arguments对象和使用方法。
npm install flat-arguments --save 用法 正常参数 var flatten = require ( 'flat-arguments' ) ; function letsDoThis ( ) { var args = flatten ( arguments ) ; // args = ['arg1', 'arg2'] } letsDoThis ( ...
log ( fixture ( 1 , 2 , 3 , { foo : 'bar' } ) )//=> [ 1, 2, 3, { foo: 'bar' } ]也可以看看 :功能齐全的 Promises/A+ 实现,性能非常好 : 处理给定的 Arguments 对象 - 单独返回最后一个参数(通常是回调)和...
$ npm install mux-arguments 用法 const mux = require ( 'mux-arguments' ) function muxDemux ( arg1 , arg2 , arg3 ) { mux ( obj => { arguments . length === 1 // true demux . apply ( null , arguments ...
主要介绍了javascript内置对象arguments,实例讲解arguments是什么怎么用,需要的朋友可以参考下
一个用于解析命令行参数的简单库。 com.audienceproject/simple-arguments_2.12/1.0.1/simple-arguments_2.12-1.0.1.jar
一个用于解析命令行参数的简单库。 com.audienceproject/simple-arguments_2.11/1.0.0/simple-arguments_2.11-1.0.0.jar
所有的函数都有一个自己的arguments对象,用来储存它实际接受到的参数,而不局限于函数声明时所定义的参数列表。它不是数组却类似数组,具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值...
small code to explain the mimo behavior
主要介绍了Javascript中的arguments对象 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
在上篇文章中我们讨论了javascript中的默认参数,这篇文章,我们来讨论下javascript的arguments参数对象。 如下例的一个函数,我们如何根据传入参数的不同来做不同的处理呢? 代码如下: function addAll () { // ...
1、在JavaScript中,arguments对象是比较特别的一个对象,实际上是当前函数的一个内置属性。arguments非常类似Array,但实际上又不是一个Array实例。可以通过如下代码得以证实(当然,实际上,在函数funcArg中,调用...
理解Javascript函数形式参数与arguments
是参数 这是参数对象吗? 这个问题比您想象的要难。...如果您已修改了实际的arguments给它一个对象Symbol.toStringTag属性,那么这个包将返回false 。 测验 只需克隆仓库, npm install ,然后运行npm test