0

Here's a trivial example of defining a "class" in JavaScript, creating an instance of it, and invoking one of it's methods/functions

// Define a class like this
function Person(name, gender){

   // Add object properties like this
   this.name = name;
   this.gender = gender;
}

// Add methods like this.  All Person objects will be able to invoke this
Person.prototype.speak = function(){
    alert("Howdy, my name is" + this.name);
};

// Instantiate new objects with 'new'
var person = new Person("Bob", "M");

// Invoke methods like this
person.speak();

The reason for the use of quotation marks in the opening sentence, is because the above is not really a class at all, because JavaScript doesn't have classes. So what's the best word to describe the above?

I know that Person is the constructor function and speak is a method, but what is the canonical term for the totality of the constructor and it's methods. Obviously in Java, C++, C#, etc. it would be called a class, but I'm not sure what the equivalent term is in JavaScript?

Dónal
  • 185,044
  • 174
  • 569
  • 824

2 Answers2

2

A constructor function would be a typical way to refer to it

However, you could call it a class and I highly doubt anyone would argue with you in a practical or workplace setting. It's still essentially a class in concept, even if Javascript doesn't implement it as such - anybody seeing that code would understand that you were making an object based on a class-like definition.

The implementation is clearly different, but the intent is (near enough) the same, and it's pretty fair to use the same name. If nothing else, it's less likely to cause confusion

I make no promises over whether someone would argue with you over it in a research or academic setting....

Jon Story
  • 2,881
  • 2
  • 25
  • 41
  • 2
    personally, I don't find the term **constructor function** very satisfactory, because this is also the term used for just the constructor of the class, i.e. the `Person` function – Dónal Jan 07 '15 at 14:31
  • 2
    It's not my favourite term either, as it does feel slightly inaccurate, but it's fairly commonly used and more commonly understood among JS devs, in my experience. In my current team we tend to just refer it to a class, as the difference isn't usually significant and we all know what we mean. – Jon Story Jan 07 '15 at 14:35
  • I usually go with TypeScript / CoffeeScript because of this fuzz ;) The constructor function - it's the closest to a class and, in practical terms, behaves like one ... to me. – BananaAcid Jan 07 '15 at 15:32
2

JavaScript doesn't have classes, but I would still call it a class. ECMAScript Harmony will introduce classes to JavaScript (although they aren't really classes). This is how your code might look in ECMAScript Harmony:

class Person {
    constructor(name, gender) {
        this.name   = name;
        this.gender = gender;
    }

    speak() {
        alert("Howdy, my name is " + this.name + ".");
    }
}

var person = new Person("Bob", "M");

person.speak();

This can be simulated in current JavaScript as follows:

var Person = defclass({
    constructor: function (name, gender) {
        this.name   = name;
        this.gender = gender;
    },
    speak: function () {
        alert("Howdy, my name is " + this.name + ".");
    }
});

var person = new Person("Bob", "M");

person.speak();

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}

As you can see the defclass function takes a prototype object and returns a “class”. Every class must have a constructor function.

There are two types of object-oriented programming languages: classical languages and prototypal languages.

Classes and prototypes are isomorphic (i.e. prototypes can be converted into classes and classes can be converted into prototypes). See the answer to the following question for more details:

How to achieve pseudo-classical inheritance right on the class declaration?

Prototype-Class Isomorphism

The above diagram is taken from the answer to the following question:

JavaScript inheritance and the constructor property

In your question you are asking for a term that describes the totality of the constructor and its methods.

Both the constructor and its methods are properties of the prototype object. Hence the canonical term for the totality is “prototype”.

However, since prototypes and classes are isomorphic, you could call it a “class” as well.

The answer to the following question explains how prototypes are isomorphic to classes:

classical inheritance vs protoypal inheritance in javascript

To recap, the term you are asking for is “prototype”. Prototypes are isomorphic to classes. Hence you can call it a “class” as well. Prototypes have a constructor method, which is usually used to create an instance of the prototype. Constructors are not the totality, but a part of the totality.

Prototypes vs Classes

Till now, I only stated that prototypes are isomorphic to classes without illustrating how, because I didn't want to duplicate my previous answers. However, some duplication is good.

As I said before there are two types of object-oriented languages: classical and prototypal. In classical languages you have two separate things for abstraction (objects) and generalization (classes).

To explain abstraction and generalization in brief:

  1. An object is an abstraction of a real world entity. For example Bob is a real world entity. In a computer program, Bob is represented using an object which has properties like name and gender. This object is not Bob. It is just an abstraction of Bob (i.e. it describes Bob).
  2. An abstraction of an abstraction is a generalization. In classical languages a generalization is represented using a class. In prototypal languages a generalization is represented using a prototype object. For example, the Person class and the Person.prototype object are both generalizations of Bob (or an abstraction of the object representing Bob).

Hence objects are abstractions of real world entities and both classes and prototypes are generalizations (i.e. abstractions of abstractions).

The difference between classical and prototypal languages is that in classical languages objects are abstractions of only real world entities. Classes are used to create abstractions of objects or other classes.

In prototypal languages objects are abstractions of both real world entities and other objects. When an object is an abstraction of another object then it is called a prototype object.

Hence, in a sense prototypes and classes are dual concepts. They represent the same thing (i.e. a generalization). This is why they are isomorphic.

Because the “totality” of the constructor and its methods you are refering to is essentially a prototype object, it can either be called simply a “prototype” or by extension a “class”. However, it cannot be called a constructor (which is just a part of the whole).

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • 1
    I believe, this is the most complete answer iv'e ever seen on this. Like my personal approach, in case of disliking prototype/"ungrouped code", i suggest using an option like the mentioned defclass function or start using TypeScript / CoffeeScript. Helped me to stay close to replicate the usual code paradigms in (my opinion) easy to-write-messy-javascript .. – BananaAcid Jan 07 '15 at 15:40