如何取消定义或删除 javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6598058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do undefined or remove a javascript function?
提问by Jammy
I defined a global Javascript function:
我定义了一个全局 Javascript 函数:
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?
谢谢。有什么更好的方法吗?
回答by jcolebrand
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
对象上,所以你只需要用那个名字重新定义窗口函数。
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.
或者,您可以将其重新定义为任何其他值,甚至可以根据需要将其重新定义为 null,但至少通过将其保留为函数,它仍然可以被“调用”而不会造成损害。
Here's a live exampleof redefining the function. (thanks TJ)
这是重新定义函数的一个活生生的例子。(感谢 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:
指出我在 window 对象上重新定义它的另一个原因是,例如,如果您有另一个具有该功能的对象,如果其成员,您可以以相同的方式在成员上定义它:
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.
无论是在 window 对象上还是在其他一些对象上,它的工作方式都是一样的。
回答by Brad Christie
how about using a var
?
使用一个怎么样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
回答by Swaroop
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.
最简单的方法是将函数(将其视为变量)设置为 null。即使您没有将其声明为var
. 在 IE 上验证了这一点。
resizeDashBoardGridTable = null
回答by Garre
If the functions needs to be called 1 time you use an anonymous self invoking function like this:
如果需要调用函数 1 次,您可以使用如下匿名自调用函数:
(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.
如果您必须多次调用该函数var
,请将其存储到 a 中并null
在完成后将其设置为。
Note: You don't have to name an anonymous function like I named it test. You can also use it like this:
注意:您不必像我将其命名为 test 那样命名匿名函数。你也可以这样使用它:
(function(){
console.log('test');
})();
The reason I do name my anonymous functions is for extra readability.
我命名匿名函数的原因是为了增加可读性。
回答by Akash
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...
祝你好运...