Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What was the rationale behind having the receiver in functions default to the global object?

Tags:

javascript

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
}
like image 695
Ben Aston Avatar asked Dec 31 '14 09:12

Ben Aston


People also ask

What does global object mean in JavaScript?

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.

What is global context in JavaScript?

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.


1 Answers

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.

like image 55
2 revs, 2 users 60% Avatar answered Sep 21 '22 07:09

2 revs, 2 users 60%