0

Probably it's because I'm misunderstanding the prototype chain, but could someone explain me the prototype structure that makes this assertion true?

console.log.prototype === console.prototype

I expect it to be something like this

console.prototype.log = function(){...}

So log has the basic function prototype. How does that prototype resolves to his parent prototype ?

I tried some things that I didn't expected to work, but they work. For example, instead of doing:

var binded = console.log.bind(console,'something');

I can do this

var otherBind = console.log.bind(console.log,'something else')
Cœur
  • 37,241
  • 25
  • 195
  • 267
Danielo515
  • 5,996
  • 4
  • 32
  • 66
  • You're looking for `Object.getPrototypeOf(console) === Object.getPrototypeOf(console.log)`, which is false (the former being `Console.prototype` and the latter being `Function.prototype`) – Bergi Sep 14 '16 at 22:31
  • I don't see how this question is duplicated with the linked one. Surely we can consider the other question contains this as it is more general, but they are not even close to being duplicates – Danielo515 Sep 15 '16 at 14:12
  • You want to know something about the structure of prototype chains, but are using `.prototype` instead of `Object.getPrototypeOf` (or the deprecated `__proto__`). The duplicate resolves your confusion about that, doesn't it? – Bergi Sep 15 '16 at 14:17

1 Answers1

4

Neither console.log nor console are class constructors, so their prototype properties are undefined. Since undefined === undefined, console.log.prototype === console.prototype is true.

Check out Reflect.getPrototypeOf(), that might be what you're looking for.

console.log.bind(console.log) means that this method will be called with console.log as this value. Calling the bound function works fine on Chrome and Node.js, but fails on Firefox (TypeError: 'log' called on an object that does not implement interface Console.). See console.log() called on object other than console behaves differently among different browsers.

Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • But then how is it possible that I can bind the console log to itself? is it maybe a circular reference or what? – Danielo515 Sep 14 '16 at 16:43
  • Hello @Gothdo. That is very weird. I tried passing console.log as callback argument hundreds of times, and it have always failed because the absence of a correct `this` context so I was forced to bind it to console. Is there any scenario where this is true? Thanks and regards – Danielo515 Sep 14 '16 at 20:18
  • Thank you @Gothdo!! It's fun to see how you have opened a new question and answered it within one hour :D Maybe you wanw to edit your answer with a link to the other one and I flag it as accepted? – Danielo515 Sep 14 '16 at 21:50