javascript Javascript删除一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16498247/
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
Javascript delete a function
提问by Akshat
How can I delete a function
如何删除一个函数
i.e
IE
test=true;
delete test;
=> true
function test() {..}
delete test()
=> false
Delete usually works for variables but it doesn't work for functions.
删除通常适用于变量,但不适用于函数。
回答by Benjamin Gruenbaum
No, you can not delete
the result of a function declaration.
不,你不能delete
得到一个函数声明的结果。
This is a part of the language specification.
这是语言规范的一部分。
If you check out the description of the delete operatorin JavaScript:
如果您查看JavaScript 中删除运算符的描述:
If desc.[[Configurable]] is true, then
Remove the own property with name P from O.
Return true.
如果 desc.[[Configurable]] 为真,则
从 O 中删除名为 P 的自己的属性。
返回真。
If you go to the browser and run the following in the console:
如果您转到浏览器并在控制台中运行以下命令:
>function f(){}
>Object.getOwnPropertyDescriptor(window,"f")
You would get:
你会得到:
Object {value: function, writable: true, enumerable: true, configurable: false}
Object {value: function, writable: true, enumerable: true, configurable: false}
What can be done:
可以做什么:
You can however, assign the result to another value that is not a function, assuming that is your last reference to that function, garbage collection will occur and it will get de-allocated.
但是,您可以将结果分配给另一个不是函数的值,假设这是您对该函数的最后一次引用,则会发生垃圾收集并取消分配。
For all purposes other than getOwnPropertyNames
hasOwnProperty
and such, something like f = undefined
should work. For those cases, you can use a functionExpression instead and assign that to a variable instead. However, for those purposes like hasOwnProperty
it will fail, try it in the console!
对于除此之外的所有目的getOwnPropertyNames
hasOwnProperty
,类似的东西f = undefined
应该可以工作。对于这些情况,您可以改用 functionExpression 并将其分配给变量。但是,对于那些hasOwnProperty
会失败的目的,请在控制台中尝试!
function f(){}
f = undefined;
window.hasOwnProperty("f");//true
Some more notes on delete
关于删除的更多注意事项
When your modern browser sees a
delete
statement, that forces it to fall to hash map mode on objects, sodelete
can be very slow (perf).In a managed language with a garbage collector, using
delete
might prove problematic. You don't have to handle your memory, the language does that for you.In the case you do want to use objects like a map, that's a valid use case and it's on the way:)
回答by SomeShinyObject
delete
only works for properties of objects. If test()
was inside an object, you could delete it, but if it's a stand alone function, you're stuck with it, unless you nullify it or define it as something else.
delete
仅适用于对象的属性。如果test()
在一个对象内,你可以删除它,但如果它是一个独立的函数,你就会被它卡住,除非你取消它或将它定义为其他东西。
Object Delete
对象删除
var obj = {
test: function() {
console.log("I'm a test");
}
}
obj.test(); //I'm a test
delete obj.test;
obj.test(); //Nothin'
Function Reassign
功能重新分配
function test() {
console.log("I'm a test");
}
test(); // I'm a test
delete test;
test = undefined;
test(); // TypeError