关于arguments.callee

zhengshuanger 2009-11-13 08:37:01
demo1

function ff(){
alert("1");
ff = function(){
alert("2");
}
}

ff(); // output 1
ff(); // output 2
ff(); // output 2
ff(); // output 2




demo2

function f(){
alert("1");
arguments.callee = function(){
alert("2");
}
}

f(); // output 1
f(); // output 1
f(); // output 1
f(); // output 1




不能给 callee 赋值吗..

并且 .如果demo1是一个匿名方法..那它的写法应该变成怎样的 ?
...全文
439 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
dh20156 2009-11-14
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhengshuanger 的回复:]
demo1
JScript codefunction ff(){
alert("1");
ff=function(){
alert("2");
}
}

ff();// output 1ff();// output 2ff();// output 2ff();// output 2

demo2
JScript codefunction f(){
alert("1");
arguments.callee=function(){
alert("2");
}
}

f();// output 1f();// output 1f();// output 1f();// output 1

不能给 callee 赋值吗..

并且 .如果demo1是一个匿名方法..那它的写法应该变成怎样的 ?
[/Quote]

JavaScript不能动态修改自身的代码,这将非常邪恶~~~``

如果一定要这样:


<script type="text/javascript">
function ff(){
alert(1);
for(var d in window){
if(window[d]==arguments.callee){window[d] = function(){alert(2);};}
}
}

ff(); // output 1
ff(); // output 2

</script>
浴火_凤凰 2009-11-14
  • 打赏
  • 举报
回复
callee 仅当相关函数正在执行时才可用。
wcwtitxu 2009-11-14
  • 打赏
  • 举报
回复

function ff() {
alert(1);
// 给 arguments.callee 赋值
arguments.callee = function() {
alert(2);
};
// 试执一下
arguments.callee(); // 看到2, 说明赋值成功.
}
ff();




function ff() {
// ff 跟 arguments.callee 都指向同一个函数
alert(ff == arguments.callee); // 看到 true

// 给 arguments.callee 赋值, 这个赋值是成功的, 但只改变 arguments.callee
arguments.callee = function() {
alert(2);
};

// 没有改变 ff 的指向
alert(ff == arguments.callee); // false
}
ff();
windsoniczyx 2009-11-14
  • 打赏
  • 举报
回复
Callee是arguments的一个成员,它指向当前正在被执行的函数。你试图改变这个值那当然是不行的了。
wcwtitxu 2009-11-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 objector 的回复:]
2. 对于demo2,如果arguments.callee不能被赋值的话,那么demo2在执行的时候很显然会报错。所以,既然你连续调用四次函数f都没有错误的话,那么可以得出结论:arguments.callee是可以被赋值的。
  但是在demo2中你对arguments.callee的操作是完全没有意义的。我知道你的本意,既然你知道arguments.callee保存的是当前函数的引用,那么你去改变它,只会改变它的指向罢了,对于函数f来说没有一点影响,因为:你改变的只是arguments.callee的引用,没有改变函数f的引用。
  故而四次调用f输出结果的都是1也就不足为奇了。
[/Quote]

个人认同
Objector 2009-11-14
  • 打赏
  • 举报
回复
1. 对于demo1,如果是匿名函数,在不将这个匿名函数的引用赋给一个变量之前,将没有任何方式去引用该函数,处理利用闭包立即执行一次(只有一次哦),如:

(function() {
alert('1');
ff = function() {
alert('2');
};
})();


2. 对于demo2,如果arguments.callee不能被赋值的话,那么demo2在执行的时候很显然会报错。所以,既然你连续调用四次函数f都没有错误的话,那么可以得出结论:arguments.callee是可以被赋值的。
但是在demo2中你对arguments.callee的操作是完全没有意义的。我知道你的本意,既然你知道arguments.callee保存的是当前函数的引用,那么你去改变它,只会改变它的指向罢了,对于函数f来说没有一点影响,因为:你改变的只是arguments.callee的引用,没有改变函数f的引用。
故而四次调用f输出结果的都是1也就不足为奇了。
friendly_ 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 cloudgamer 的回复:]
callee 属性是 arguments 对象的一个成员,仅当相关函数正在执行时才可用。

你这是给一个属性赋值,跟f无关啊
[/Quote]
up
xiaofan_sap 2009-11-13
  • 打赏
  • 举报
回复
lz 看看这篇文章 就明白了
cloudgamer 2009-11-13
  • 打赏
  • 举报
回复
callee 属性是 arguments 对象的一个成员,仅当相关函数正在执行时才可用。

你这是给一个属性赋值,跟f无关啊
Click_Me 2009-11-13
  • 打赏
  • 举报
回复

// 你第一个DEMO的理解就有问题了
function ff(){
alert("1");
aa = function(){
alert("2");
}
}

ff(); // output 1
aa(); // output 2
aa(); // output 2
aa(); // output 2

//-->
</script>

87,916

社区成员

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

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