demo1function ff(){
alert("1");
ff = function(){
alert("2");
}
}ff(); // output 1
ff(); // output 2
ff(); // output 2
ff(); // output 2
demo2function f(){
alert("1");
arguments.callee = function(){
alert("2");
}
}f(); // output 1
f(); // output 1
f(); // output 1
f(); // output 1
不能给 callee 赋值吗..并且 .如果demo1是一个匿名方法..那它的写法应该变成怎样的 ?

解决方案 »

  1.   


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

  2.   

    callee 属性是 arguments 对象的一个成员,仅当相关函数正在执行时才可用。你这是给一个属性赋值,跟f无关啊
      

  3.   


    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>
      

  4.   

    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也就不足为奇了。
      

  5.   

    Callee是arguments的一个成员,它指向当前正在被执行的函数。你试图改变这个值那当然是不行的了。
      

  6.   


    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();
      

  7.   

    callee 仅当相关函数正在执行时才可用。