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