Search code examples
javascriptclassecmascript-5

Classes in JavaScript ECMAScript 5?


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?


Solution

  • 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.