Intoduction: Javascript Object
Objects are real things we see and interact with in our
daily life. They are things like animals, human begins,
houses, vehicles, books and things like that. Objects in
javascript have properties. For example a 'book' as an object
has the following properties:
title, author, publisher,publication date, subject etc.
Also objects have methods like walk, work, eat, sleep and so on.
Method is the actions performed by the object.
How to define objects
In javascript, object can be defined in many ways, that is,
it can be defined using many methods.
Object can be defined by using object literal, using
'new' keyword, by using object constructor and by
using object.create and by using classes (thisis introduced by es6).
All these afore-mentioned methods would be dealth with in this tutorial.
Object literal is defined, using the following notation:
name : value pair
surrounded by curly braces, much like a
dictionary in python. Let's take book as example of our object
with 7 properties and add property value to each property:
const book = {
title : "As You Like It",
author : "Shakespear Williams",
place-of-publication : Heinemann,
publisher : "Male",
date-of-publication : "London"
pagination: "300p.",
subject: Literature - drama"
};
Creating object by literal
Empty Object Creation
Object can also be created empty, having no values at the time
of its creation and later would be populated with values like this
const book = {};
title : "As You Like It",
author : "Shakespear Williams",
place-of-publication : Heinemann,
publisher : "Male",
date-of-publication : "London"
pagination: "300p.",
subject: Literature - drama"
Empty object creation by object literal method
New keyword object creation
Another method for creating an object is by using 'new' keyword.
see the example below:
const book = new book{
title : "As You Like It",
author : "Shakespear Williams",
place-of-publication :London,
publisher : "Heinemann",
date-of-publication : "1901"
pagination: "300p.",
subject: Literature - drama"
};
object literal, new keyword method
Object is mostly defined in javascript by using object literal method.
This is because of its simplicity, readability and execution speed.
Object in javascript is much alike with python dictionary and association
array in Php.
Object Creation By Constructor
Another method of creating object is by using object constructor function.
This is made possible with aid of new keyword, this type of object creation
allows multiple objects to be created within the program. Follow these two steps
in order to create the object, using constructor function:
spep 1. create a function and pass in the object properties as parameters into
the function.
step 2. Use "new" keyword to create the objects.
See the code below for example.
function book(title, author, pubPlace, publisher, pubDate,
pagination, subject) {
this.title = title;
this.author = author;
this.pubPlace = pubPlace;
friends.publisher = publisher;
this.pubDate = PubDate;
this.pagination = pagination;
this.subject = subject;
}
const book1 = new book("As You like it", "Shakespear Williams", "London",
"Heinemann", "1914", "xvii, 300 pages", "Literature - drama");
const book2 = new book("The Man Die", "Soyinka Wole", "London",
"Longman", "1954", "xvi, 295 pages", "Literature - prose");
object object constructor function
Explanation
Javascript function constructor object is created with and named "book",
inside the constructor, the object properties were passed as parameters, which are
title of the book, author of the book, place of publication, publisher, and the year
of publication of the book object. However, the book object properties is useless until
the real book is created and reference the each of these properties, hence that nessecitated
for the creation of two books - book1 and book2. We use "new" keyword to create the books and
pass in the property values as arguments, so that when each of these books is called, complete
information of the book will be displayed.
Essence of creating and object is to store data in it and proccess the
data whenever the need for it arises. It will be a total time and energy
wastage when data are stored in a vault and the data content of such a vult could not be accessed
for updating and modification. Right in this chapter, it is
worth to note that objects in javascript are mutable, meaning that
the contents of the object can be changed. You can add, delete, and Modify
the values when the need for that becomes necessary. In the next article,
we are going to see how to access values in our book object and the various method of
displaying its data.