From: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented\_JS
To start with, let's give you a simplistic, high-level view of what Object-oriented programming (OOP) is. We say simplistic, because OOP can quickly get very complicated, and giving it a full treatment now would probably confuse more than help. The basic idea of OOP is that we use objects to model real world things that we want to represent inside our programs, and/or provide a simple way to access functionality that would otherwise be hard or impossible to make use of.
Objects can contain related data and code, which represent information about the thing you are trying to model, and functionality or behavior that you want it to have. Object data (and often, functions too) can be stored neatly (the official word isencapsulated) inside an object package (which can be given a specific name to refer to, which is sometimes called anamespace), making it easy to structure and access; objects are also commonly used as data stores that can be easily sent across the network.
Let's consider a simple program that displays information about the students and teachers at a school. Here we'll look at OOP theory in general, not in the context of any specific programming language.
To start this off, we could return to our Person object type from ourfirst objects article, which defines the generic data and functionality of a person. There are lots of things you_could_know about a person (their address, height, shoe size, DNA profile, passport number, significant personality traits ...) , but in this case we are only interested in showing their name, age, gender, and interests, and we also want to be able to write a short introduction about them based on this data, and get them to say hello. This is known asabstraction— creating a simple model of a more complex thing, which represents its most important aspects in a way that is easy to work with for our program's purposes.
In some OOP languages, this generic object type definition is called aclass(JavaScript uses a different mechanism and terminology, as you'll see below) — it isn't actually an object, rather it is a template that defines what characteristics an object should have.
From our class, we can createobject instances— objects that contain the data and functionality defined in the class. From our Person class, we can now create some actual people:
When an object instance is created from a class, the class'sconstructor functionis run to create it. This process of creating an object instance from a class is calledinstantiation— the object instance isinstantiatedfrom the class.
In this case we don't want generic people — we want teachers and students, which are both more specific types of people. In OOP, we can create new classes based on other classes — these newchild classescan be made toinheritthe data and code features of theirparent class, so you can reuse functionality common to all the object types rather than having to duplicate it. Where functionality differs between classes, you can define specialized features directly on them as needed.
This is really useful — teachers and students share many common features such as name, gender, and age, so it is convenient to only have to define those features once. You can also define the same feature separately in different classes, as each definition of that feature will be in a different namespace. For example, a student's greeting might be of the form "Yo, I'm [firstName]" (e.gYo, I'm Sam), whereas a teacher might use something more formal, such as "Hello, my name is [Prefix] [lastName], and I teach [Subject]." (e.gHello, My name is Mr Griffiths, and I teach Chemistry).
Note: The fancy word for the ability of multiple object types to implement the same functionality ispolymorphism. Just in case you were wondering.
You can now create object instances from your child classes. For example:
In the rest of the article, we'll start to look at how OOP theory can be put into practice in JavaScript.
Some people argue that JavaScript is not a true object-oriented language — for example, it doesn't have aclass
statement for creating classes, unlike many OO languages. JavaScript, instead, uses special functions calledconstructor functionsto define objects and their features. They are useful because you'll often come across situations in which you don't know how many objects you will be creating; constructors provide the means to create as many objects as you need in an effective way, attaching data and functions to them as required.
When a new object instance is created from a constructor function, the functionality is not all copied over to the new object like "classic" OO languages — instead the functionality is linked to via a reference chain called a prototype chain (seeObject prototypes). So this is not true instantiation, strictly speaking — JavaScript uses a different mechanism to share functionality between objects.
Note: Not being "classic OOP" is not necessarily a bad thing; as mentioned above, OOP can get very complex very quickly, and JavaScript has some nice ways to take advantage of OO features without having to get too deep into it.
Let's explore creating classes via constructors and creating object instances from them in JavaScript. First of all, we'd like you to make a new local copy of theoojs.htmlfile we saw in our first Objects article.
script
element:function createNewPerson(name) {
var obj = {};
obj.name = name;
obj.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
return obj;
}
You can now create a new person by calling this function — try the following lines in your browser's JavaScript console:
var salva = createNewPerson('Salva');
salva.name;
salva.greeting();
This works well enough, but it is a bit longwinded; if we know we want to create an object, why do we need to explicitly create a new empty object and return it? Fortunately JavaScript provides us with a handy shortcut, in the form of constructor functions — let's make one now!
function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
}
The constructor function is JavaScript's version of a class. You'll notice that it has all the features you'd expect in a function, although it doesn't return anything or explicitly create an object — it basically just defines properties and methods. You'll see thethis
keyword being used here as well — it is basically saying that whenever one of these object instances is created, the object'sname
property will be equal to the name value passed to the constructor call, and thegreeting()
method will use the name value passed to the constructor call too.
Note: A constructor function name usually starts with a capital letter — this convention is used to make constructor functions easier to recognize in code.
So how do we call a constructor to create some objects?
var person1 = new Person('Bob');
var person2 = new Person('Sarah');
person1.name
person1.greeting()
person2.name
person2.greeting()
Cool! You'll now see that we have two new objects on the page, each of which is stored under a different namespace — when you access their properties and methods, you have to start calls withperson1
orperson2
; they are neatly packaged away so they won't clash with other functionality. They do, however, have the samename
property andgreeting()
method available. Note that they are using their ownname
value that was assigned to them when they were created; this is one reason why it is very important to usethis
, so they will use their own values, and not some other value.
Let's look at the constructor calls again:
var person1 = new Person('Bob');
var person2 = new Person('Sarah');
In each case, thenew
keyword is used to tell the browser we want to create a new object instance, followed by the function name with its required parameters contained in parentheses, and the result is stored in a variable — very similar to how a standard function is called. Each instance is created according to this definition:
function Person(name) {
this.name = name;
this.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
}
After the new objects have been created, theperson1
andperson2
variables contain the following objects:
{
name: 'Bob',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
{
name: 'Sarah',
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
}
Note that when we are calling our constructor function, we are defininggreeting()
every time, which isn't ideal. To avoid this, we can define functions on the prototype instead, which we will look at later.
The example we looked at above was only a simple example to get us started. Let's now get on and create our finalPerson()
constructor function.
function Person(first, last, age, gender, interests) {
this.name = {
first,
last
};
this.age = age;
this.gender = gender;
this.interests = interests;
this.bio = function() {
alert(this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. He likes ' + this.interests[0] + ' and ' + this.interests[1] + '.');
};
this.greeting = function() {
alert('Hi! I\'m ' + this.name.first + '.');
};
}
var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);
You'll now see that you can access the properties and methods just like we did with the first object we defined:
person1['age']
person1.interests[1]
person1.bio()
// etc.
Note: If you are having trouble getting this to work, try comparing your code against our version — seeoojs-class-finished.html(alsosee it running live).
To start with, try adding a couple more object creation lines of your own, and try getting and setting the members of the resulting object instances.
In addition, there are a couple of problems with ourbio()
method — the output always includes the pronoun "He", even if your person is female, or some other preferred gender classification. And the bio will only include two interests, even if more are listed in theinterests
array. Can you work out how to fix this in the class definition (constructor)? You can put any code you like inside a constructor (you'll probably need a few conditionals and a loop). Think about how the sentences should be structured differently depending on gender, and depending on whether the number of listed interests is 1, 2, or more than 2.
Note: If you get stuck, we have provided ananswer inside our GitHub repo(see it live) — try writing it yourself first though!
So far we've seen two different ways to create an object instance —declaring an object literal, and using a constructor function (see above).
These make sense, but there are other ways — we want to make you familiar with these in case you come across them in your travels around the Web.
First of all, you can use theObject()
constructor to create a new object. Yes, even generic objects have a constructor, which generates an empty object.
var person1 = new Object();
person1.name = 'Chris';
person1['age'] = 38;
person1.greeting = function() {
alert('Hi! I\'m ' + this.name + '.');
};
var person1 = new Object({
name: 'Chris',
age: 38,
greeting: function() {
alert('Hi! I\'m ' + this.name + '.');
}
});
Constructors can help you give your code order—you can create constructors in one place, then create instances as needed, and it is clear where they came from.
However, some people prefer to create object instances without first creating constructors, especially if they are creating only a few instances of an object. JavaScript has a built-in method calledcreate()
that
allows you to do that. With it, you can create a new object based on any existing object.
var person2 = Object.create(person1);
person2.name
person2.greeting()
You'll see thatperson2
has been created based onperson1
—it has the same properties and method available to it.
One limitation ofcreate()
is that IE8 does not support it. So constructors may be more effective if you want to support older browsers.
We'll explore the effects ofcreate()
in more detail later on.
This article has provided a simplified view of object-oriented theory — this isn't the whole story, but it gives you an idea of what we are dealing with here. In addition, we have started to look at how JavaScript relates to and how it differs from "classic OOP", how to implement classes in JavaScript using constructor functions, and different ways of generating object instances.