Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in Firefox Function.prototype.func=... doesn't affect console.log?

I have added a function using Function.prototype.func = ... to Function but in Firefox it has not been added console.log:

Function.prototype.func = function () { return this.toString(); };
alert(typeof console.log.func); // in FF: undefined, in Chrome: function

Is it a bug or is there any reason for that?

like image 280
Ali Shakiba Avatar asked May 03 '13 16:05

Ali Shakiba


1 Answers

It is quite telling that in Firefox:

var foo = function() {}
foo.__proto__ == Function.prototype;

is true, whereas the statements:

console.log.__proto__ == Function.prototype;
console.log instanceof Function;

are both false.

Thus, console.log does not include Function.prototype in its prototype chain, so altering Function.prototype has no affect on console.log. This is perfectly fine, since console is a host object (rather than a native object in the ECMAScript specification) and may behave however Mozilla (or Google, or Microsoft, etc.) would like.

Why does this behavior exist? I'm not a Firefox dev, so I can't say for certain, but my best guess is that this was done specifically because console is a debugging tool. If you muck around with Function's prototype chain and then want to use console.log to verify what you're doing, it would be terrible if your debug reporting tool itself starting messing up and misreporting things to you.

EDIT:

The console functions have a separate prototype chain used by all of them:

console.log.__proto__ == console.dir.__proto__ // true
console.log.__proto__.func = 5;
console.dir.__proto__.func == 5  // true
like image 62
apsillers Avatar answered Nov 08 '22 16:11

apsillers