Look at the following code:
function doNotCallMe(){
alert("Otherwise the world will be destroyed!");
}
function getNotCallable(){
return new function() {
alert("Attention!");
doNotCallMe();
};
}
var not_callable = getNotCallable();
The browser shows the alerts, which it shouldn’t be. Why? How to fix?
is what you want instead.
All functions in JavaScript will act as object constructors when used with operator
new; so you’re defining an anonymous function, and then invoking it throughnew.