Thomas said:
Garrett said:
A safe way to remove an existing event handler property is to replace
the value with a function the does nothing and returns undefined. The
global noop function `Function.prototype` fits that bill perfectly.
Example:
hostObject[methodName] = Function.prototype;
ECMA-262 states:
| The Function prototype object is itself a Function object (its
| [[Class]] is "Function") that, when invoked, accepts any arguments
| and returns undefined.
Function.prototype returns nothing.
Implementation of Function.prototype that fulfills that could only be
unsafe if it were to add some side effects.
The same could be said for String, or any other function.
Such side effects would be taking on additional nonstandard
functionality. Calling Function.prototype with the expectation that it
has no observable side effects is as safe as calling String, parseInt,
isNaN with the same expectation.
Function.prototype does nothing and returns nothing.
When the program wants a function that does nothing and returns nothing,
Function.prototype is a good candidate for that.
var noop = Function.prototype; // Reusable everywhere.
One case where Function.prototype function cannot be used is for new
expressions; Function.prototype is not specified to implement
[[Construct]], and so a TypeError can be expected. It is unsafe to use
Function.prototype function in reusable constructor patterns such as
those used for prototype inheritance.
var f = Function.prototype;
f.prototype = MyCtor.prototype;
var i = new f; // TypeError