4
$\begingroup$

So there's this adage that says "Objects are a poor man's closures", which can equally well be reversed, which I understand to mean that they are equivalently expressive.

What strikes me about the semantics of JavaScript, is that this adage seems to come way closer to home, due to these two facts:

  • Function execution frames are scopes that essentially reside on the heap (depending on the implementation is of course), because they're closures. Without something like escape analysis you cannot throw the scope away after the function ends executing. I.e. the classical distinction stack/heap is no more.
  • But more importantly, JavaScript's prototypal inheritance works the same way as lexical variable lookup.

The only "real" difference between scopes and objects in JavaScript seems to be that whereas you can programmatically make new objects with a given prototype, you cannot programmatically make a new scope with a given lexical parent.

My question then:

  • Would it make sense to void the distinction altogether?
  • Have people seriously considered this before, maybe discarded the idea due to some really obvious problems?

To be clear, I'd be talking about a language like this:

// (1) A scope is like an "object builder", and it's lexical
//     surrounding is like the object prototype chain

let adjective = "awesome";
let o = { let name = "objects"; let adjective = "boring" };
let s = { let name = "scopes" };
let describe = concept => `${concept.name} are ${concept.adjective}`
console.log( [o, s].map(describe) )
// ["objects are boring", "scopes are awesome"]

// (2) An example of what inheritance would probably look like

let Animal = numLegs => { let canSpeak = false }
let cat = Animal(4)
// cat.canSpeak == false, cat.numLegs == 2

let Person = name => { extend Animal(2); canSpeak = true }
let kelley = Person("Kelley")
// kelley.canSpeak == true, kelley.numLegs == 2, kelley.name = "Kelley"
```
$\endgroup$
  • 1
    $\begingroup$ Are you the guy who wrote the page you mentioned? $\endgroup$ – Apass.Jack Jul 29 at 21:36
  • $\begingroup$ For sure :) I'm going to write a mini transpiler out of curiosity, work with the language a bit, say rewriting the transpiler in it, and see whether it's a really bad idea or not haha $\endgroup$ – Kelley van Evert Jul 30 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.