12

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
   }

Thanks. any better way?

6
  • 1
    "the Procedure code should be called again" What does that mean? Commented Jul 6, 2011 at 14:30
  • 2
    Just do: resizeDashBoardGridTable = function(){} Commented Jul 6, 2011 at 14:30
  • Try if this works? Call the JS function: eval('function resizeDashBoardGridTable(gridID){ ; }') It doesn't 'undefine' it but it does make it do nothing. Commented Jul 6, 2011 at 14:31
  • @Cipi @Angad ~ You should at least attach it to the global object, in case he calls that inside a closure or somesuch (and the use of eval here ... tsk tsk) Commented Jul 6, 2011 at 14:34
  • @Angad: No need for eval (here, or virtually anywhere). Commented Jul 6, 2011 at 14:35

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I added a live example, just for clarity.
@T.J.Crowder - I know it's been a few years, but sadly, the JSBin example (jsbin.com/imejob) has been removed (expired).
5

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

3 Comments

Thanks, I don't know change this function, change it will bring some risk.
@Jammy: I was just pointing out that as a variable you have full control. Alternatively, you can use the window.<function> (if defined globally) like jcolebrand mentioned and do it that way, too.
@Jammy: Redefining it as @Brad suggests does change things a bit, you're right to be cautious. Specifically, it changes when that function initially gets defined (upon entry to global scope, as in your code; or later when the step-by-step execution of code reaches the assignment, in Brad's code). A lot of the time it doesn't matter, but that's okay, there's no need to change your definition, you can still redefine the symbol the way you're declaring the function.
3

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

Comments

1

If the function 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.

Comments

0

The simplest approach is to set the function (treat it as a variable) increment/decrement (++ or --). This works even if you don't declare it as a var.

resizeDashBoardGridTable++;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.