Is it possible to modify a function itself when its property function is called?

前端 未结 6 1204
遥遥无期
遥遥无期 2021-01-28 13:02

Basically I want to do this:

someFunction() // do something

someFunction.somePropertyFunction()

someFunction()  // Now someFunction is modified; it should now          


        
6条回答
  •  一整个雨季
    2021-01-28 13:19

    Sort of:

    function someFunction() {
        return realFunction.apply(this, arguments);
    }
    
    function someFunctionA(name) {
        return 'Hello, ' + name + '!';
    }
    
    function someFunctionB(name) {
        return 'Goodbye, ' + name + '...';
    }
    
    var realFunction = someFunctionA;
    
    someFunction.somePropertyFunction = function () {
        realFunction = someFunctionB;
    };
    

提交回复
热议问题