Object-oriented – How come javascript, being a prototype based language, doesn’t have an easy way to access the prototype

javascriptobject-orientedprototyping

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).

Best Answer

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/

Related Topic