3

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?

6
  • When you use class in ES2015 (ES6), you're not doing anything you can't do in ES5—you're just doing it with less typing. So the answer is yes, to the extent that there are classes in ES2015, there are also classes in ES5. Commented Aug 9, 2015 at 19:38
  • BI know that it is possible to rebuild classes and emulate them. But are they really already part of the programming language? On the official EMCA website it says "ECMAScript does not use classes such as those in C++, Smalltalk, or Java.". Commented Aug 9, 2015 at 19:40
  • 1
    ECMA may be referring to Javascript classes being prototype-based rather than inheritance-based. Commented Aug 9, 2015 at 19:44
  • class is just syntactic sugar for a pattern that is common in ES5. It doesn't add any new semantics to the language, and defining a class with the class keyword is semantically identical to defining a class in ES5 by creating a prototype and then adding methods to that prototype and so on. Classes in ES5 and classes in ES2015 are the same, it just takes less typing to declare one in ES2015. JavaScript classes are different from Java classes, but so too are Java classes different from C++ classes or Smalltalk classes. Commented Aug 9, 2015 at 19:48
  • @Jordan, so too are Java classes different from C++ classes. Are you referring to generics vs. templates? Because otherwise Java classes are much more related to C++ classes than Javascript classes are. Commented Aug 9, 2015 at 19:50

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

17 Comments

We're certainly not calling prototypes (the objects) classes :-)
@JavaForAndroid: No, JavaScript is not class-based. It's object-based.
@Bergi, I'm sorry to learn I'm not a member of the "JS community". May I inquire where you have found a definite consensus against the use of "class" in JS?
@FrédéricHamidi: I'm not saying that the js community doesn't use the term "class" (see my own answer above), I only meant that when saying "class" we don't refer to the prototype object. Most people mean the entireness of the constructor function, the prototype object, it's methods, the shared inheritance, and the usage with new. All what makes the implementation of the concept (and I admit there are multiple ways to implement the concept, so it's not always as clear)
@JavaForAndroid Yes and no. From the point of a Java or C++ programmer it is a "pseudo-class". From the point of a JavaScript programmer it is a class. If you are comparing JavaScript classes to Java or C++ classes, they are definitely "pseudo-classes". The classes can be modified, you can also modify the objects and even delete "methods".
|
2

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

Comments

1

From MDN:

Classes are syntax sugar over constructor functions

You can use this new construction in ES5 by using some compiler:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.