如何取消定义或删除javascript函数

如何取消定义或删除javascript函数

本文介绍了如何取消定义或删除javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个全局Javascript函数:

I defined a global Javascript function:

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

使用此函数后次,我想删除(或未定义)此函数,因为应该再次调用过程代码。如果有人试图调用这种方法我们什么都不做。

After this function was used a few times, I want to remove(or undefined) this function because the Procedure code should be called again. if somebody try to call this method we need do nothing.

我现在不能改变这个功能。

I don't way change this function right now.

所以重新定义这个函数可能是一种方式:

so re-defined this function may be one way:

  function resizeDashBoardGridTable(gridID){
      empty,do nothing
   }

谢谢。更好的方法吗?

推荐答案

因为你是全局声明的,所以它附加在窗口 object,所以你只需要用该名称重新定义窗口函数。

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.

这是一个。 (感谢TJ)

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.

这篇关于如何取消定义或删除javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:36