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
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 Sep 21 '15 at 22:51this
into the function. That'll log the value ofthis
as it is outside of theconsole.log()
function, not the value ofthis
inside. – Pointy Sep 21 '15 at 22:51