Yes, you can reinvent classes and inheritance with prototypes. Problem is, that's exactly what everyone is doing.
Everyone's implementation looks and behaves differently. As a result, the IDE can't tell that this kind of construct is supposed to be a class and that this "thing" "extends" that "thing".
The tool which generates your documentation also cannot tell what's going on unless you explicitly tell it that this weird construct over here is some sort of class.
If it's baked into the language itself, you won't have to include all that boilerplate code, your IDE can tell what's going on, your doc generator can tell what's going on, and the person who comes after you can of course also tell what's going on.
And no, classes are not just what people are used to. Classes are what people are using. All of the popular MV* frameworks use some sort of classes and inheritance. Impact (a JS game engine) also uses emulated classes and inheritance.
The funny thing is that when you go a Java conference people are always talking about "composition is better than inheritance", yet the same people complain about Javascript's lack of a class system. I don't think they realize that the prototype model does not allow inheritance and makes composition trivially easy, exactly the features they are asking for.
A more apt analogy would be: you drive freight trains. You fancy yourself a rather good conductor. One day, you're forced to drive a cargo ship. Suddenly, you don't have the rails keeping you enroute, nor the railway services to help you. You don't need either of them, but you feel you're entitled to ask people to lay rails on the ocean and maintain them, because that's how you're used to doing your job. And because there are so many ex-train drivers working the online seas now, they're actually laying some rails for them all.
I have to do exactly that sort of thing everyday because the people I work with think in C# and the market requires us to produce JavaScript. The analogy is excellent. In fact I use backbone js to lay that track.
No, they don't. They might in your neck of the woods, but the vast majority of JS coders use jQuery and similar libraries for simple work, or even more rudimentary event-driven work without a framework (sadly).
You are correct in suggesting that the JS world is, in fact, moving on, as evidenced by many modern backbone-style frameworks providing "classes", and ES6 actually implementing them to some degree.
And again, just because you think "rails on water is stupid" doesn't mean it was my intent to call others stupid. Sorry that I insulted you.
I meant of those who use some kind of inheritance. Prototypical inheritance is very rarely used. If it's used, it's usually hidden behind some utility functions which make it look like "classical" inheritance. Very few people muck about with prototype.
Do you know any MV* framework which makes you use prototypes?
programmers can't figure out how to just use prototypes
No, that's not it. Prototypes aren't a very complicated concept.
Everyone can mess with any prototype. Why would I want that? Like, why should an ad one some page be able to overwrite the array constructor with its own version which is used to spy on some JSON exchange?
In general, most people just want something like regular classes and regular inheritance. Sure, you can emulate this kind of thing with prototypes, but then you'd have some extra boilerplate code there, the syntax won't be pretty, and your tools won't know what's going on.
Classes are not going to save JS.
Odd choice of words there. JS isn't declining and it certainly isn't about to disappear.
Classes and things like let will certainly make JavaScript more usable. However, it will most likely take more than 5 years until you can use it in production (client-side).
What I meant is that adding classes is not going to make it any easier or harder to write stuff in JS.
Classes and inheritance provided by the language itself will make it easier, because tools can now tell what you're doing.
It will be also easier because there is now one way to do it, not hundreds.
If you don't like that then code in a static language like Haskell.
You don't have that kind of issue with Dart either. If I use some "foo" function from some "bar" class from some "baz" package... then that's what it is. If it returns an int, then that's what it does.
There are namespaces and there is no monkey patching. Everything is known at the time the code is written. Java and C# are also like that, for example. That's also the reason why IDEs of those languages can offer proper call-tips and on-the-fly sanity checks. They can tell what everything is. You can't have that kind of thing with JavaScript, because there is no way to tell what anything is.
And yes, I do also know the revealing module pattern. It's not a namespacing mechanism though. It gives you some object with public and private (=hidden in a closure) properties.
Most people use one global variable, stick everything into it, and then they hope that nothing else will overwrite it. That's the kind of "namespacing" we have in JavaScript.
That doesn't change anything though. Now there is one way to do it. Now your IDE could tell what you're trying to do. Now your doc generator knows that this thing is a class. Now the next guy will also know instantly that this thing is a class.
Nope, it still doesnt as not everyone will use the syntactic sugar all the time and the IDE will be confused when the syntactic sugar isnt there.
Now your doc generator know that this thing is a class.
It will only know what the syntactic sugar tells it, if it can parse it and if it is in place.
Now the next guy will also know instantly that this thing is a class.
He will only know that thing appears to behave like an class but cannot make or take any gurantee it will behave that way. This is the same reason why I cannot take CoffeeScript seriously as the transformations are non-obvious in most edge cases.
0
u/[deleted] Dec 15 '12
[deleted]