How do undefined or remove a javascript function?

后端 未结 5 891
不知归路
不知归路 2021-02-06 00:05

I defined a global Javascript function:

  function resizeDashBoardGridTable(gridID){
  var table = document.getElementById(\'treegrid_\'+gridID);
        .....
          


        
相关标签:
5条回答
  • 2021-02-06 00:17

    how about using a var?

    // define it
    var myFunction = function(a,b,c){
      console.log('Version one: ' + [a,b,c].join(','));
    }
    myFunction('foo','bar','foobar'); // output: Version one: foo,bar,foobar
    
    // remove it
    myFunction = null;
    try { myFunction(); console.log('myFunction exists'); }
    catch (e) { console.log('myFunction does not exist'); }
    
    // re-define it
    myFunction = function(d,e,f){
      console.log('Version two: ' + [d,e,f].join(','));
    }
    myFunction('foo','bar','foobar'); // output: Version two: foo,bar,foobar
    

    OUTPUT:

    [10:43:24.437] Version one: foo,bar,foobar
    [10:43:24.439] myFunction does not exist
    [10:43:24.440] Version two: foo,bar,foobar
    
    0 讨论(0)
  • 2021-02-06 00:22

    Because you're declaring it globally, it's attached to the window object, so you just need to redefine the window function with that name.

    window.resizeDashBoardGridTable = function() {
      return false;
    }
    

    Alternately you could redefine it to any other value or even to null if you wanted, but at least by keeping it a function, it can still be "called" with no detriment.

    Here's a live example of redefining the function. (thanks TJ)

    An additional reason for pointing out that I'm redefining it on the window object is, for instance, if you have another object that has that function as one if its members, you could define it on the member in the same way:

    var myObject = {};
    myObject.myFunction = function(passed){ doSomething(passed); }
    ///
    /// many lines of code later after using myObject.myFunction(values)
    ///
    /// or defined in some other function _on_ myObject
    ///
    myObject.myFunction = function(passed){}
    

    It works the same either way, whether it's on the window object or some other object.

    0 讨论(0)
  • 2021-02-06 00:27

    If the functions needs to be called 1 time you use an anonymous self invoking function like this:

    (function test(){
        console.log('yay i'm anonymous');
    })();
    

    If you have to call the function multiple times you store it into a var and set it to null when you're done.

    Note: You don't have to name an anonymous function like I named it test. You can also use it like this:

    (function(){
        console.log('test');
    })();
    

    The reason I do name my anonymous functions is for extra readability.

    0 讨论(0)
  • 2021-02-06 00:28

    This is what i'm doing:

    var someGlobalVariable = 'some-value';
    
    // work with someGlobalVariable...
    
    // once done, set it to a Reference Error
    someGlobalVariable = new ReferenceError('This variable is undefined.');
    
    // now if we try to access:
    console.log(someGlobalVariable); // ReferenceError: This variable is undefined
    

    Good Luck...

    0 讨论(0)
  • 2021-02-06 00:32

    The simplest approach is to set the function (treat it as a variable) to null. This works even if you don't declare it as a var. Verified this on IE.

    resizeDashBoardGridTable = null
    
    0 讨论(0)
提交回复
热议问题