6

May be this is a stupid question, but I'm kind of intrigued.

Being JavaScript a prototype based language, with its pseudo-class function constructors sort of half baked (remember JavaScript: The Good Parts ...) I wonder how is it possible that it didn't have, right from the start, some easy way to access the prototype of each object.

By easy way I mean something like object.prototype, or object._prototype (and not the dirty proto)

For what I know, the Object.getPrototypeOf(obj) was just added in late 2008, almost 13 years after JavaScript was launched.

I'm asking this because I always wondered if JavaScript was ever meant to be used as a pure prototype based language, I mean, without using any function to construct objects (in other words, never using the new operator).

9

Look up Object.create for an alternative to function constructors which I actually find very useful since they give you encapsulated instance vars via closure.

I'm not sure how long it's been available but I've been using <object>.constructor.prototype for some time now.

But really, JS was written in 10 days. It's been evolving from that rush job ever since. I'm not even sure they had prototype on function constructors when it was first released. Netscape's top brass wanted it to look like Java. Brendan Eich wanted it to work like Scheme (and decided he was making it look like C rather than Java, by which I assume he's stating he wasn't a huge fan even back then).

Also, Good Parts is a decent read but it's not the ultimate authority and Crockford has some really weird hang-ups about the language. Function constructors in particular which he's claimed are bad because you might forget to use new keyword and screw up. Well, you might forget to wear your pants in the morning too but you tend to notice pretty quick.

Took me a bit to find this but if you really want to know how it came about, it's best to go to the horse's mouth:

https://brendaneich.com/2008/04/popularity/

  • "Brendan Eich wanted it to work like Scheme": Interesting, why not use Scheme directly, then? – Giorgio Jun 13 '13 at 5:25
  • 1
    Also, needed to look like Java because that's what the cool kids were using in '94. Yes. They were cool once. Well, not really. – Erik Reppen Jun 13 '13 at 5:30
  • 3
    None of us are cool @Giorgio. None of us. – Erik Reppen Jun 13 '13 at 6:49
  • 6
    @Giorgio: He was actually hired to design a Scheme dialect and write an implementation for embedded web scripting. But the higher-ups at Netscape suddenly changed their mind, and said that they wanted an object-oriented language like Java. And that they wanted to ship within two weeks. So, Eich did the simplest thing possible: he stayed as close to Scheme as he could, took some ideas from the simplest OO languages he knew (NewtonScript, Act-1, Self), replaced the ubiquitous "list" data structure from Scheme with a ubiquitous "dictionary" (and called it "object" to fool his bosses). … – Jörg W Mittag Jun 13 '13 at 10:21
  • 4
    … and sprinkled the whole thing with curly braces so that nobody would notice that he actually still designed a Scheme dialect ;-) – Jörg W Mittag Jun 13 '13 at 10:22
0

This is really a question about what went through Eich's mind when he designed the language, so it's only really him who can answer.

But if I should venture a guess, I would say the initial version of JavaScript was a quick-and-dirty prototype and he expected to be able to iron out inconsistencies and missing features in subsequent releases. But then it was quickly reverse-engineered with bug-for-bug compatibility by Microsoft and then standardized, which suddenly froze the language earlier than expected.

I'm pretty sure it was intended that instances should be created with 'new' like in Java, and it was more "an under the hood" implementation detail that inheritance happened through prototypes. The keywords 'extends' and 'implements' was reserved from the beginning, which hints that the language was expected to develop a more java-like syntax for inheritance. But until then, you had to munge with prototypes directly to use inheritance.

So the current situation is kind of half baked, and not the result of a deliberate design decision.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.