This code works, in some cases. If it returns console.log then the call to p.out functions just fine.
function Notice(mode) {
this.debug = mode;
this.out = (function() {
if(mode) {
if(window.console) {
return console.log;
} else {
return alert;
}
} else {
return Notice.doNothing;
}
})(mode);
}
var p = new Notice('1');
p.out('Kool-aid, OH YEAH!');
However, when it returns alert (or window.alert) I get an error:
Error: uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame :: http:// .. :: <TOP_LEVEL> :: line 22" data: no]
And as a simple test, this works:
out = (function() { return alert; })();
out('hello dolly');
How can I get obj.out to function properly when its being set to alert?
Why not return an anonymous function that in return calls the build-in function?
Something like