JavaScript function redefinition

后端 未结 6 1325
梦毁少年i
梦毁少年i 2020-12-15 06:51

Is it possible to redefine a JavaScript function from within its own body. For example, could I do the following?

function never_called_again(args) {
  // Do         


        
6条回答
  •  无人及你
    2020-12-15 07:22

    Yes, it's possible, and it will not create a global function. I verified this in Internet Explorer 6, Firefox, Chrome, and Opera. Consider the following code:

      
         Never called again
         
    
         
    
      
      
    
         
      
    

    This will print "Hello World {function code}" the first time of the never_called_again and the second time it will print "hello {changed function code}" the second time.

    But when it's being called on button's onclick, it will throw an error saying that the function is not defined, clearly indicating that the function was re-defined (and not created globally).

提交回复
热议问题