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

前端 未结 6 1206
遥遥无期
遥遥无期 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:31

    Easily. Here's an example:

    var derp = 123;
    someFunction = function() {alert(derp);};
    someFunction.somePropertyFunction = function() {derp = 456;};
    
    someFunction(); // alerts 123
    someFunction.somePropertyFunction();
    someFunction(); // alerts 456
    

    Okay, that's an oversimplified example, but yeah, it's entirely possible.

提交回复
热议问题