Why in Firefox Function.prototype.func = ... does not affect console.log?

I added a function using Function.prototype.func = ... to Function , but in Firefox it was not added by console.log :

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

Is this a mistake or is there a reason for this?

+6
source share
1 answer

It’s clear that in Firefox:

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

is true , while the statements are:

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

are false .

Therefore, console.log does not include Function.prototype in its prototype chain, so changing Function.prototype does not affect console.log . This is fine because console is a host object (and not a native object in the ECMAScript specification) and can behave, however, by Mozilla (or Google or Microsoft, etc.).

Why does this behavior exist? I am not a Firefox developer, so I can’t say for sure, but I think it was done on purpose, because console is a debugging tool. If you cheat on the Function prototype chain and then want to use console.log to check what you are doing, it would be terrible if your debugging reporting tool started to get confused and distort something for 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 
+2
source

Source: https://habr.com/ru/post/944233/


All Articles