It seems that __defineGetter__ just fails. Object.defineProperty gives an error message:
redefine_disallowed
when you call this in Chrome:
Object.defineProperty(window, "undefined", { get: function() { return 2; } });
On the other hand:
window.__defineGetter__("undefined", function() { return 2; }); window.__lookupGetter__("undefined");
Why it works in the Chrome console:
When you define and get undefined at a time when you paste the code into the console, it works because some functions that use the with block referring to console._commandLineAPI are executed behind the scenes:
if (injectCommandLineAPI && inspectedWindow.console) { inspectedWindow.console._commandLineAPI = new CommandLineAPI( this._commandLineAPIImpl, isEvalOnCallFrame ? object : null ); expression = "with ((window && window.console && window.console._commandLineAPI) || {}) {\n" + expression + "\n}"; } return evalFunction.call(object, expression);
So you just define console._commandLineAPI.undefined .
Another point is that it overwrites console._commandLineAPI (see the code above), so it does not work if you define and receive undefined as two commands, since the getter was thrown by correspondence at the time of the attempt to get undefined .
This, as well as the fact that it does not overwrite window.undefined , most likely it works in the console.
source share