Can you alter a Javascript function after declaring it?

前端 未结 12 818
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 22:10

Let\'s say I have var a = function() { return 1; }. Is it possible to alter a so that a() returns 2? Perhaps by editing a

12条回答
  •  悲哀的现实
    2020-11-28 22:34

    You can change functions like other objects

    var a1 = function(){return 1;}
    var b1 = a1;
    a1 = function(){
      return b1() + 1;
    };
    console.log(a1()); // return 2
    
    // OR:
    function a2(){return 1;}
    var b2 = a2;
    a2 = function(){
      return b2() + 1;
    };
    console.log(a2()); // return 2

提交回复
热议问题