发现一个 function arguments 非常有趣的问题(散分)
<script type="text/javascript">
/*<![CDATA[*/
function test_f(o, i, a)
{
var argument_o = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<p />' );
o = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<hr />' );
}
function test_f_1(i, a, o)
{
var argument_o = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<p />' );
o = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<hr />' );
}
function test_f_2(i, a, o)
{
var argument_o = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<p />' );
arguments[2] = {};
argument_o= arguments_to_object_f(arguments);
for(var i in argument_o)
{
document.write( argument_o[i], '<br />' );
}
document.write( '<hr />' );
}
document.write( 'test_f(undefined, 1, [1])<p />' );
test_f(undefined, 1, [1]);
document.write( 'test_f_1(1, [1]);<p />' );
test_f_1(1, [1]);
document.write( 'test_f_2(1, [1]);<p />' );
test_f_2(1, [1]);
function arguments_to_object_f(argument_o)
{
var result_o = {};
var argument_a = [];
argument_o.callee.toString().replace
(
/\((.*?)\)/
, function($0, $1){ argument_a = $1.replace(/\s+/g, '').split(',') }
);
for( var i=0, j=argument_a.length; i<j; i++ )
{
result_o[ argument_a[i] ] = argument_o[i];
}
return result_o;
}/* function arguments_to_object_f(argument_o) */
/*]]>*/
</script>请说说 test_f_1, test_f_2 为什么会是那样的执行结果...
相关推荐
浅谈javascript中的Function和Arguments
javascript的Function 属性: 1、Arguments对象 2、caller 对调用单前函数的Function的引用,如果是顶层代码调用, 则返回null(firefox返回undefined)。 注:只有在代码执行时才有意义 3、length 声明函数是指定的命名参数的个数(函数定义是,定义参数的个数) 4、prototype 一个对象,用于构造函数,这个对象定义的属性和方法 由构造函数创建的所有对象共享。 方法: applay() –> applay(this,[]) call() –> call(this,可变参数) toString() ja
TypeError: _queue_reduction(): incompatible function arguments.
使用pytorch的DDP分布式训练的时候遇到错误: TypeError: _queue_reduction(): incompatible function arguments. The following argument types are supported: 1. (process_group: torch.distributed.ProcessGroup, grads_batch: List[List[at::Tensor]], devices: List[int]) -> Tuple[torch.distributed.Work, at::Tensor] Invoked
深入解析JavaScript中的arguments对象
arguments定义 所有的函数都有一个自己的arguments对象,用来储存它实际接受到的参数,而不局限于函数声明时所定义的参数列表。它不是数组却类似数组,具有数组一样的访问性质及方式,可以由arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length。但是却不具有数组的一些方法。可以通过call把arguments转化成真正的数组,然后进行数组的操作。 var args = Array.prototype.slice.call(arguments); 类数组 1. 判断ARGUMENTS是不是数组 alert(arguments instanceof Arra
javascript 利用arguments实现可变长参数
javascript arguments解释,实现可变长参数。 在C#中,有可变长参数params[],但是在js中,如何实现这种可变参数呢? 一、可变长参数 arguments是非常好的解决方法,一直不知道javascript有这个东西。 先来看看应用场景,使用arguments传入任意个数的参数到js函数里的写法。 function Test() { console.log(arguments[0]); console.log(arguments[1]); console.log(arguments[2]); }; Test(1, 2, 3); 输出 1 2 3; 当然,你也
简单谈谈Javascript函数中的arguments
一、arguments的面貌 在javascript中所有的函数内部都包含了一个隐藏的变量叫arguments;它存放着所有传递到这个函数中的参数; 那么我们打开实例看看arguments的输出形式 (function fn(){ console.log(arguments) })(1,2,3,4) 结果好像是类似数组的形式打印在控制台,相信大多数人包括我看到这种输出就会认为arguments是一个数组,那么既然是数组就可以用数组的一些方法了吧,再看下一个例子; (function fn(){ arguments.push(5) console.log(arguments) })



