I'm just getting into advanced contextual instantiation, using prototype chain etc.
I'm curious how the window.console
object is created such that the function log
thinks it's context is the window instance, not the console. Is this something to do with Object.create
the new
keyword, or binding / self = this?
window.console
has a constructor (Console) and I'm curious what the cleanest way to invoke the constructor, passing Window instance/context would be? Pass it in as a param?? If Console is a seperate constructor, I think Window would be the one constructing it with a new context, rather than saying
windowInstance.console.log = function (args) {
}.bind(windowInstance, args);
Pretty much, I'm imagining a layout like this, but don't understand how this
gets routed
var window = new Window();
window.console.log(this); // logs window
function Window () {
// this === window when constructed above
this.console = new Console();
}
function Console () {
this.log = function () {} // where this === window.console but log thinks it's window
}
Thanks
I'm just getting into advanced contextual instantiation, using prototype chain etc.
I'm curious how the window.console
object is created such that the function log
thinks it's context is the window instance, not the console. Is this something to do with Object.create
the new
keyword, or binding / self = this?
window.console
has a constructor (Console) and I'm curious what the cleanest way to invoke the constructor, passing Window instance/context would be? Pass it in as a param?? If Console is a seperate constructor, I think Window would be the one constructing it with a new context, rather than saying
windowInstance.console.log = function (args) {
}.bind(windowInstance, args);
Pretty much, I'm imagining a layout like this, but don't understand how this
gets routed
var window = new Window();
window.console.log(this); // logs window
function Window () {
// this === window when constructed above
this.console = new Console();
}
function Console () {
this.log = function () {} // where this === window.console but log thinks it's window
}
Thanks
Share Improve this question asked Sep 21, 2015 at 22:47 neaumusicneaumusic 10.5k11 gold badges59 silver badges86 bronze badges 6- Your code logs nothing: jsfiddle/nn3dq1L6 It's not obvious what "but log thinks it's window" is based on. – zerkms Commented Sep 21, 2015 at 22:50
-
3
Its that way for everything, not just
console.log
. I could be wrong, but as far as I can tell, the scope ofthis
matters during execution; if it were executed from within a function, it'd have the parent asthis
, however, you're executing it in the scope of the window. – Daedalus Commented Sep 21, 2015 at 22:51 -
4
You're passing
this
into the function. That'll log the value ofthis
as it is outside of theconsole.log()
function, not the value ofthis
inside. – Pointy Commented Sep 21, 2015 at 22:51 - 2 @Pointy I tried to say that, but my wording is unfortunately not as efficient as yours :/ – Daedalus Commented Sep 21, 2015 at 22:52
- 1 @Daedalus ha ha, no I saw your ment after mine was typed in and I agree you're saying the same thing. – Pointy Commented Sep 21, 2015 at 22:54
1 Answer
Reset to default 5If you had:
var obj = { hello: "world" };
console.log(obj);
would you be surprised that the console showed that object? Well by the same token,
console.log(this);
logs the value of this
as it is outside the call to the console function. You can't force console.log()
to log what it thinks the value of this
is in its own frame of reference; there's just no provision for that in the API (because frankly it's not very useful). If you want to log the state of the window.console
object however you can:
console.log(window.console);