42
I am studying Javascript and saw some explanations that left me with doubts. Here at Stackoverflow I saw several questions about the possibility of creating an abstract class in Javascript.
But my question is this:: It is possible to create a Javascript class?
I’m asking this because I’m using it as a guide the website Developer.Mozilla found the following information:
"Javascript is a dynamic object-oriented language; it has types and operators, objects and methods. Its syntax comes from the Java languages and C, so many structures of these languages apply to Javascript also. One of the main differences is that Javascript has no classes; instead, the class functionality is made by prototypes of objects. The other main difference is that functions are objects, giving the functions the ability to store executable code and be passed as parameter to any other object."
The statement in bold left me with this doubt, because it is a reliable source for studies. What I understood is that there are no classes, but there are elements that can be used as classes.
I don’t know if that’s the correct interpretation.
What’s more, this same text says that Javascript is object-oriented, so I tried to understand why the statement of the non-existence of classes would be there.
I see many co-workers and professionals outside who say they instantiated Javascript classes, used such a class for such a purpose in Javascript.
With that I want to understand, it is correct to state that there are classes in Javascript or not?
Perfect, that’s what it is.
– bfavaretto
So what works as a class in javascript is not exactly the same as language classes like java, C# etc? That’s it?
– DiChrist
Yes. You program object-oriented, such as, but internally the behavior of a class in Javascript is different than in Java and C#. I think the main difference is that in Java and C# class are abstract entities, that is, when I instate an object, they are created according to the class specification, the object is concrete, but the class is something abstract. In JS no, a class is also an object, it exists and instance.
– Silvio Lucena Junior
I think that comment (that should be part of the answer) very good. However I do not think that Javascript loses the right to call Class which ES6 added to the language just because it doesn’t behave with classical languages.
– Sergio
@Sergio Your argument is valid. In fact, the quoted excerpt of the specification is not normative, that is, it is only an explanation about the functioning of language. If we accept that "class" does not presuppose a certain kind of inheritance implementation, we can use the term in JS. But I personally find it confusing, and I think the new syntax should not even have been created (they will swear to me for this, hehe).
– bfavaretto
I also don’t think @Sergio, I actually think that this difference has a small impact on our everyday life as a developer. It’s more of an internal characteristic of language in my opinion. But it also allows us to face a certain problem from a different point of view because the JS prototype chain is subtle and powerful. I think I can say that in JS we can do everything that a classical object-oriented language does, and a few other things that they don’t do.
– Silvio Lucena Junior
In short, we can use Javascript classes yes, we can program object-oriented Javascript yes. The only thing is that internally javascript classes are different from Java classes, but they behave basically the same.
– Silvio Lucena Junior
@bfavaretto I also think it should not have been created, so Mootools continued to reign alone :)
@Silvio
I agree, and did not say before but to say contrary to what I said in my reply I think it is quite valid this reply+1
.– Sergio
Well, I think I understand better now. Of all the questions the bfavaretto was the one that helped me the most, but this comment you answered right after mine is very enlightening. I think my doubt is almost resolved. I will read again all the answers, compare them and see which one helped me most to accept as the answer to my question, because it can help others. But regardless of the answer chosen, all are very good and well explained. OBS.: Put your comment in the reply :)
– DiChrist
There is an important advantage to this syntax for "fake" classes: IDES code helper tools can offer you better suggestions when they know what’s going on.
– Pablo Almeida