28

How can I delete a function

i.e

test=true;
delete test;
=> true

function test() {..}

delete test()
=> false

Delete usually works for variables but it doesn't work for functions.

5
  • 2
    Duplicate post stackoverflow.com/questions/6598058/… Commented May 11, 2013 at 14:41
  • 2
    I checked that out before posting, its not the same. Im also not using jquery & i want to delete it not just declare it to null Commented May 11, 2013 at 14:43
  • 2
    Why do you want to do it? Commented May 11, 2013 at 14:46
  • 3
    If you set it to null it's as good as deleted since the function itself will no longer exist in memory (assuming you have no other references to it, and noting that actual garbage collection may not be instant). Commented May 11, 2013 at 14:46
  • 1
    Why do you want to do this? What's the actual goal? I suspect that deleting the function is not necessary to reach it. Commented May 11, 2013 at 14:51

3 Answers 3

29

No, you can not delete the result of a function declaration.

This is a part of the language specification.

If you check out the description of the delete operator in JavaScript:

If desc.[[Configurable]] is true, then

  • Remove the own property with name P from O.

  • Return true.

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}

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!

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

Some more notes on delete

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

2 Comments

@alex23 Note that I never claim that a function can't be deleted but that a function declaration can't be deleted. Function declarations (I linked to the spec) are when you do something like function myFunction(){ and not when you do var a = function(){ or obj.property = function(){ (Those are variable declarations and object property declarations, followed by assignment to a function expression ). It's a good point though, the fact my answer wasn't clear to you means it probably won't be clear to other people too, which makes this (and your) comments useful :)
I obviously meant "function declared with a function declaration", a function declaration is a part of code :)
13

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.

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

2 Comments

You are invoking the function inside the delete statement in your second example, I assume that's not intentional?
if you write testfunc=undefined the variable testfunc is still a variable with no content. Better: testfunc=null;
5

You could always do:

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

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

testFunc = undefined;

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

3 Comments

What about testFunc = undefined;, wouldn't that be more appropriate?
@jgillich neither produces the same result as delete but out of the two undefined is more appropriate
Dont work correct this method : function fn(){ return 5 }var x = fn; delete x; x=undefined undefined fn() 5

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.