In early versions of JavaScript, what was the rationale behind having the receiver
(aka context
) default to the global object?
function a() {
console.log(this); // window
}
A global object is an object that always exists in the global scope. In JavaScript, there's always a global object defined. In a web browser, when scripts create global variables defined with the var keyword, they're created as members of the global object.
The Javascript global scope is the context where everything in a Javascript program executes by default. This scope includes all variables, objects, and references that are not contained within a customized scope defined by a programmer. Global scope is the entire Javascript execution environment.
Brendan Eich answered:
So that top-level functions (the only kind in first release) can act as window or frame (later, iframe) methods. Still used.
JS jargon nit: "receiver" is the standard OO term in languages that influenced JS, rather than highly-overloaded "context".
Question posed to Brendan Eich:
So ES1 did not have methods? (cf ur comment on top level fns)
Brendan Eich:
No, JS1 in '95 (no "ES" then) had methods via function-valued properties, but all functions were top level. ES1 standardized.
ES3 in '99 added function expressions & nested functions (closures), which I didn't have time to implement in those 10 days.
To go back to orig q: in window w, function m(){} makes w.m() callable from other reachable windows/frames with
this
== w.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With