Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript delete a function

Tags:

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.

like image 807
Tarang Avatar asked May 11 '13 14:05

Tarang


People also ask

Is there a delete function in JavaScript?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

How do you use Delete function?

Use the DELETE function to erase the data contents of a specified field, value, or subvalue and its corresponding delimiter from a dynamic array. The DELETE function returns the contents of the dynamic array with the specified data removed without changing the actual value of the dynamic array. dynamic.

What is the function of delete operator?

The delete operator in JavaScript is used to delete an object's property. If it is used to delete an object property that already exists, it returns true and removes the property from the object.

How do I remove something from JavaScript?

The only way to fully remove the properties of an object in JavaScript is by using delete operator. If the property which you're trying to delete doesn't exist, delete won't have any effect and can return true.


1 Answers

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

  • When your modern browser sees a delete statement, that forces it to fall to hash map mode on objects, so delete 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 :)

like image 143
Benjamin Gruenbaum Avatar answered Oct 07 '22 17:10

Benjamin Gruenbaum