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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:55:26  来源:igfitidea点击:

Javascript delete a function

javascript

提问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 deletethe 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 getOwnPropertyNameshasOwnPropertyand such, something like f = undefinedshould work. For those cases, you can use a functionExpression instead and assign that to a variable instead. However, for those purposes like hasOwnPropertyit will fail, try it in the console!

对于除此之外的所有目的getOwnPropertyNameshasOwnProperty,类似的东西f = undefined应该可以工作。对于这些情况,您可以改用 functionExpression 并将其分配给变量。但是,对于那些hasOwnProperty会失败的目的,请在控制台中尝试!

function f(){}
f = undefined;
window.hasOwnProperty("f");//true

Some more notes on delete

关于删除的更多注意事项

回答by SomeShinyObject

deleteonly 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

回答by adamjc

You could always do:

你总是可以这样做:

var testFunc = func() 
{
    // stuff here
}

//...
testFunc();
//...

testFunc = undefined;

delete in JavaScript has no bearing on freeing memory, see here

JavaScript 中的 delete 与释放内存无关,请参见此处