In this post, It says that the delete operator does not work on functions.
The invocation of the delete operator returns true when it emoves a property and false otherwise. it’s only effective on an object’s properties, it has no effect on variable or function names.
But consider this code:
delete Date;
new Date(); // thros an reference error
To make sure that Date is a function constructor, I wrote:
typeof Date; // "function"
This piece of code verifies that Date is a constructor, not an object. If the delete operator work on this particular type of function constructor, it should work on my constructor also. But it doesn't.
function a(){}
var b = new a();
delete a; // false
So, the delete operator did not delete my function constructor but for some reason, it deleted the Date constructor. Can someone clear this confusion of mine?
If you are doing this in a browser context then Date is a property on window, which is what you actually deleted. That the value of that property is a constructor/function doesn't really matter, the property is gone and most likely that was your only reference to that function.
This still works and returns a date object.
var dateFn = Date;
delete Date;
new dateFn();
Because it was only the property that was removed and not the actual function.