It all start with an object.
An object is a thing that we interact with, it has properties and methods. The object is the heart of object-oriented programming, not only for JavaScript but also for Python
, Swift
, Java
and others. Stop thinking about individual variable and functions and start thinking in term of self-sufficient object.
Here's a list of concepts that are most often used when talking about object-oriented programming(OOP):
Object, property, and method
object, property and method
object literal
Create a new object in JavaScript by setting its properties inside curly braces. Object literals property values can be any data typers, like functin literals, arrays, string, numbers, or boolean.
Let's create an object with a named book, with propertiest such as author, published year, title, and a method ---summary
const book = {
title: 'Hippie',
author: 'Paulo coelho',
year: '2020'
}
After creating an object you can get the value with dot notation. For example, we can get the value of the title with book.title
. We can also access the properties with square backets: book['title']
.
####Object constructor
Object constructor is the same as regular fucntion. it will be called each time an object is create. We can use them with the new
keyword. Object constructor is useful when we want to create multiple objects with the same properties and methods.
const book = {
title: 'Hippie',
author: 'Paulor coelho',
year: '2018;
}
const book1 = {
title: 'The Hippie',
author: 'Paulor coelho',
year: '1988;
}
If we want to create multiple book objects we have to duplicate the code for each book. We can keep creating books butit's kind of a pain --- the object constructor helps us to reuse the object literal.
function Book(title, author, year) {
this.title = title
this.author = author
this.year = year
}
const book1 = new Book ('Hippie', 'Paulo Coelho', '2018')
console.log(book1)
//if yu want to create nore than one book just we call function book with new keyword.
const book2 = new Book ('The Alchemist', 'Paulo Coelho', '1998')
console.log(book2)
book1
and book2
create an instance of Book and assigned it to a variable. To find out whether an obejct is an instance of another one. We can use instanceof
.
book1 instanceof of Book
> true
####Obecjt.create()
Every object javaScript will create from the master Object
. whenever we use object with a capital 'O' it refers to the master object. We can print the master object in the console, The master object has the number of methods and here we are going to see Object.create()
method.
The Obejct.create()
method creates a new object, using an existing object as the protoype. Here's the basic syntax:
Obejct.create(proto, [propetiesObejct])
proto
si the prototype of the newly-create object. propertiesObejct
iis an optional one.
Let's take simple example:
const Book = {
summary: funtion() {
console.log(`${this.title} is written by ${this.author}.`)
}
}
const book1 = Object.create(Book)
book1.title = 'Paulo Coelho'
book1.author = 'Hippie'
console.log(book1.summary())
Inthe above example, we create a prototype obejct book1
and assigned a value for author and title. We can see the summary function inside the proto object: