I would like to get to know if there are classes in JavaScript ECMAScript 5? I read some literature and there are different opinions about it. I know that it is possible to emulate classes, but I am not sure if they are called classes. Do you maybe also have some proofs?
Are these classes simply pseudo classes or what is the correct term for it?
JavaScript/ECMAScript is a prototype-based programming language.
Wikipedia’s definition of this is:
Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as prototypal, prototype-oriented, classless, or instance-based programming.
Well, know it is your decision whether to call them classes.
But some people in the JS world call them classes and with ECMAScript 6 there will be a keyword to create such prototypes, called class
. (Which implies that these prototypes should be called class.)
Since you are writing a paper, you could take a definition of class (there are a few out there), mention which you used and whether that is applicable in JavaScript. (If that somehow belongs to your work.)
Some definitions of class are also summarized in a great book by John C. Mitchell. (Mitchell, John: Concepts in Programming Languages. Cambridge University Press, 2003.)
On classes in Simula he writes: (p. 326)
Class: A Simula class is a procedure that returns a pointer to its activation record. The body of a class may initialize the object it creates.
On classes in Smalltalk he writes: (p. 327)
Class: A Smalltalk class defines class variables, class methods, and the instance methods that are shared by all objects of the class. At run time, the class data structure contains pointers to an instance variable temple, a method dictionary, and the superclass.
He also states that the concept of classes in Java and C++ is very similar to the concept in Smalltalk, which was a OO pioneer.
We can see that class can be defined differently, but both definitions above don’t seem to quite match JavaScript prototypes.
"class" is a term for many things. The classes you are looking for are a concept, and are certainly used in JavaScript. We implement the concept by reification, which is easily possible with JavaScripts powerful, object-oriented prototype inheritance. This model is quite different from the object model used in other programming languages, where classes are more a compiler artifact than concrete objects.
That the concept is implemented by means of generic language features allows for multiple slightly different implementations (some of which even don't rely on prototypes). So when the term "class" is used, we don't know how exactly it would look like. Only if we refer to a specific class with an implementation, it's clear what structure is meant (assuming your familiar with the kind of implementation).
There is however a quite standard pattern (implementation) with a constructor function that is invoked with new
to create instances, and a prototype object that is used for sharing methods. The pattern also defines how to setup inheritance and subclassing.
You're right that there is no syntactical class
structure in ES5 (though the keyword is reserved, and implemented in ES6 by the standard structure).
From MDN:
JavaScript classes are introduced in ECMAScript 6 and are syntactical
sugar over JavaScript's existing prototype-based inheritance.
You can use this new construction in ES5 by using some compiler:
- Babel
- Traceur
- JSX
- TypeScript