getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
177.9k stars 33.39k forks source link

The reason for JS being a prototype language? #1763

Closed unnsteinngardars closed 2 years ago

unnsteinngardars commented 2 years ago

I already tried to search for this but did not find anything that matched my question. I am reading the second edition of the first book and when I was reading the chapter about polyfills, the following question popped in my head.

Is JS a prototype based language partially or wholly for the reason to easily support polyfills?

Looking forward to hearing from you guys what you think.

stngeo commented 2 years ago

Hi Team, The unsubscribe function doesn’t work. I want to unsubscribe. Pls help me with this.

Best, Stanislav Georgiiev

On 20 Oct 2021, at 12:35, unnsteinngardars @.***> wrote:

 Closed #1763.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

getify commented 2 years ago

@stngeo you may be "watching" the repository, in which case you're auto-subscribed to every new issue. If you undo your watching, it should stop subscribing you.

getify commented 2 years ago

Definitely @brendaneich may have more insight, but I believe the reason for JS being prototype based (as opposed to static class inheritance like Java) was in large part to make it a simpler and lighter-weight "kid brother" to the heavier class-centric Java. That probably made his quick 10-day implementation work more feasible.

Also, I think the Self language was one of several main inspirations for JS, and it's prototype based, so I'm pretty sure that had a big part in the decision.

unnsteinngardars commented 2 years ago

Thanks for the reply, but in theory, would it not be more problematic to add polyfills for forwards compatibility in a class based language?

Given a class Foo with the method bar and you would need to make a method baz forward compatible, you would need to extend the class and add the method that would require changing the code at the time when baz would be available.

Unlike with an array method for example in JS where you just add it to the prototype and when it becomes available you just remove the polyfill.

getify commented 2 years ago

It may or may not have been possible (or hard!) to polyfill without prototypal inheritance... but I doubt that polyfills were being contemplated at all during the initial design of the language.

unnsteinngardars commented 2 years ago

Making it a lucky accident I guess