How do undefined or remove a javascript function?

后端 未结 5 895
不知归路
不知归路 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: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.

提交回复
热议问题