发现一个 function arguments 非常有趣的问题(散分)

btbtd 2008-09-26 11:57:44
<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 为什么会是那样的执行结果...
...全文
294 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
lupanlupan 2008-09-27
  • 打赏
  • 举报
回复
接分
关注
ITCamel 2008-09-26
  • 打赏
  • 举报
回复
接分,接着顶,然后再看看
btbtd 2008-09-26
  • 打赏
  • 举报
回复
7f: 那三个函数只是用来测试的, 传的参数也是针对不同条件, 没有别的意义..
  • 打赏
  • 举报
回复
接分,顺道顶
街头小贩 2008-09-26
  • 打赏
  • 举报
回复
我该传入什么参数呢?
btbtd 2008-09-26
  • 打赏
  • 举报
回复
5f: 这个一点也不简单....
jackyBody 2008-09-26
  • 打赏
  • 举报
回复
研究下,看起来比较简单!
tiyuzhongxin789 2008-09-26
  • 打赏
  • 举报
回复
楼主真有意思 我试一下结果先
eolyou 2008-09-26
  • 打赏
  • 举报
回复
arguments[2] = [];

  • 打赏
  • 举报
回复
哈哈 楼主真有意思 我试一下结果先
btbtd 2008-09-26
  • 打赏
  • 举报
回复

还有一个问题...
就是在 test_f_2 中虽然 arguments[2] = {};
但是 参数 o 依然还是 undefined
btbtd 2008-09-26
  • 打赏
  • 举报
回复
最终的实现代码...
<script type="text/javascript">
/*<![CDATA[*/

/*
eval(arguments_to_object_f.toString());
var argument_o= arguments_to_object_f(arguments);

版本: 2008-9-26 13:32:01
作用: 把 function arguments 数组 以对象方式返回
必填参数: ___argument_o
*/
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]||eval( ___argument_a[___i] );
}

return ___result_o;
}/* function arguments_to_object_f(___argument_o) */


function test_f(o, i, a)
{
o = {};

eval(arguments_to_object_f.toString());
var argument_o= arguments_to_object_f(arguments);

for(var i in argument_o)
{
document.write( i, ': ', argument_o[i], '<br />' );
}
document.write( '<hr />' );
}

document.write( 'test_f(undefined, 1, [1])<p />' );
test_f(undefined, 1, [1]);

/*]]>*/
</script>
jacklinchen 2008-09-26
  • 打赏
  • 举报
回复
先jf再看
btbtd 2008-09-26
  • 打赏
  • 举报
回复

所有才有了后面的解决方法
test_f 的 eval(arguments_to_object_f.toString()); 是把 函数 arguments_to_object_f 的变量作用域改为 test_f
.................
btbtd 2008-09-26
  • 打赏
  • 举报
回复
我的目的是无论传多少个参数, 都可以达到目的...
arguments 默认是只有传参进去才会有 arguments[x]
否则的话在 function arguments_to_object_f(argument_o) 中无法读取所有参数...

这个代码要实现的就是复制 test_f 等函数的所有参数...
比如 test_f(o, i, a) 中 只用 test_f(some, some)
则 a 在 arguments_to_object_f 中是不可见的
如果在 test_f 中再给 a 赋值, a 在 arguments_to_object_f 还是不可的

如果 使用 arguments[2] 给 a 赋值, 则 a 在 arguments_to_object_f 是可见的
但是 a 在 test_f 中是不可见的

换句话说, 要 a 在 test_f 和 arguments_to_object_f 都可见的话, 要这样:
a = some
arguments[2] = some
才可以使 a 在 test_f 和 arguments_to_object_f 中是可见的
但是假如我有一百个参数, 难道我要一个一个设置?

arguments_to_object_f 就是为了解决无论什么条件都可以自动拷贝所有参数, 无论是否有传值给函数

所有才有了后面的解决方法
test_f 的 argument_o= arguments_to_object_f(arguments); 是把 函数 arguments_to_object_f 的变量作用域改为 test_f
...
这个又要一大堆话...懒得说了

MyTools_Studio 2008-09-26
  • 打赏
  • 举报
回复
呵呵,不明白这有什么不好理解的

对27楼输出
===========================================
test_f(undefined, 1, [1])
undefined
1
1
//这个很好理解...

[object Object]
1
1
//这个通过ooo改变了arguments[0]

===========================================
test_f_1(1, [1]);
1
1
undefined
//由于只传递了两个参数,arguments[2]自然为undefined

1
1
undefined
//由于只传递了两个参数,ooo并不引用arguments[2],改变ooo对arguments[2]没影响

============================================
test_f_2(1, [1]);
1
1
undefined
//同样的,只传递了两个参数,arguments[2]为undefined

1
1
[object Object]
//直接改变arguments[2],所以结果如上
btbtd 2008-09-26
  • 打赏
  • 举报
回复
27f: 你仔细点看...貌似变量命名导致错误这样的低级问题会难得了我?
根本问题是在 arguments 这个对象, 这个是相当古怪的对象....
btbtd 2008-09-26
  • 打赏
  • 举报
回复
21f: 这是个测试的代码, 你这样说貌似有点鸡蛋里捣骨头...
而且变量多乍了..
如果让你看设计模式那你是不是一头撞死好了...

下面是我把某人用C#写的设计模式转换为 JS写法....
http://www.btbtd.org/test/sqJsTools/design_patterns/
MyTools_Studio 2008-09-26
  • 打赏
  • 举报
回复
其实是挺简单的问题,LZ变量用的太混了,参数变量i和循环计数i其实是同一个

改下变量名后就ok了


<META HTTP-EQUIV="MSThemeCompatible" CONTENT="Yes">

<script type="text/javascript">
/*<![CDATA[*/

function test_f(ooo, iii, aaa)
{

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 />' );

ooo = {};
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(iii, aaa, ooo)
{
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 />' );

ooo = {};
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(iii, aaa, ooo)
{
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(undefined, 1, [1])
undefined
1
1


[object Object]
1
1



--------------------------------------------------------------------------------
test_f_1(1, [1]);
1
1
undefined


1
1
undefined



--------------------------------------------------------------------------------
test_f_2(1, [1]);
1
1
undefined


1
1
[object Object]



--------------------------------------------------------------------------------
MyTools_Studio 2008-09-26
  • 打赏
  • 举报
回复
nod,眼睛都看花了 ^_^
[Quote=引用 21 楼 tantaiyizu 的回复:]
变量加这么多的_做什么呢? 很不好的习惯。。
[/Quote]
加载更多回复(15)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧