Assuming you can't remove or comment out that code, no, you can't prevent the function from being created.
You can, though, disconnect the function from the hello
symbol:
hello = undefined;
Now you can't call the function via that symbol anymore, and if it was the only reference to the function, the function is eligible for GC.
Example: Live Copy | Source
function hello() { console.log("Hello"); }
console.log("Before setting <code>hello = undefined;</code>");
try {
hello();
}
catch (e1) {
console.log("Exception on 'before' call: " + (e1.message || String(eq)));
}
hello = undefined;
console.log("After setting <code>hello = undefined;</code>");
try {
hello();
}
catch (e2) {
console.log("Exception on 'after' call: " + (e2.message || String(eq)));
}
Output:
Before setting hello = undefined;
Hello
After setting hello = undefined;
Exception on 'after' call: undefined is not a function
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…