Objects in JavaScript | Javascript Objects and their properties and methods
One of the key features of Javascript is its ability to work with objects, which can be used to store and manipulate data in a structured way. Now, we will explore some of the most important concepts related to Javascript objects, including properties, methods, constructors, etc.
So, to store the multiple values or information we uses objects...
What is an Object ?
In real life, an object is a realtime entity which has some special
properties and functional methods.
We have already know that, the variables are stores the data or values in
memory location, so we can say that a variable is a container of data
values.
But a variable can stores a single data value in it at a time. we canno't
able to store more than one data value in a variable. That's why we creates objects.
Object Properties :
Javascript object properties are the data stored within an object. They can be accessed and modified using the dot notation (object.property) or the bracket notation (object["property"]).
Properties can be defined when an object is created, or they can be added later on.
A object are also the variables, but they can contains many values.
Suppose we have a real time object 'bike', a bike has properties like
'colour', 'model', 'weight', and 'name'.
By using this properties, we can easily able to differentiate the number
of bikes.
Example :
const bike =
{
"Model":"JAWA 42",
"Color":"Red",
"Weight":"200KG",
"Name":"Bobber"
};
document.getElementById("name").innerHTML = bike.Name;
document.getElementById("model").innerHTML = bike.Model;
document.getElementById("colour").innerHTML = bike["Color"];
document.getElementById("weight").innerHTML = bike["Weight"];
Here this example shows a object 'bike' and their properties...
In that we are called the properties of object person by using dot(.)
operator and square brackets [ ] .
Accessing properties using dot(.) operator
object.propertyName;
Accessing Properties using square brackets [ ]
object["propertyName"];
Object Methods :
Javascript object methods are functions that are stored within an object.
They can be used to perform actions or calculations on the object's properties. Methods can be called using the dot notation, just like properties.
Objects are also has methods, that can be performed on object.
A method of object is nothing but the function defined in properties.
const person = {
firstName:"Rohan",
lastName:"Bhoi",
Age : 19,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
Here we have created a object 'person' and then properties like firsName, lastName, Age, and fullName.
But in the property fullName can contains a function, which can returns the concatenation of firstName and lastName properties.
In that we have used this keyword to access the object person.
The "this" keyword in JavaScript refers to the current context or object that the code is being executed on. The value of "this" can change depending on the context in which it is used. Read more...
To access the methods of an object, we uses methodName( )
objectName.methodName();
Example :person.fullName();
It's mandatory to use the parentheses ( ) to access the methods on an object, when you access the methods of an object without parentheses ( ), then it will returns the function defination.Example :
person.fullName;
Constructors of Objects :
A constructor of an object is nothing but a special function that can creates the multiple objects having same properties and methods of any object that is already created by us.
A constructor of object is created by using the new keyword of javascript.
const rohan = new Person(values);
Example : // Function for object person
function Person(first, last, age)
{
this.firstName = first;
this.lastName = last;
this.Age = age;
this.fullName = function()
{
return this.firstName+" "+this.lastName;
}
};
// Creating constructor
const rohan = new Person("Rohan","Bhoi",19);
After that, we used this keyword to refer the object Person which has three values.
And then we are created the properties of object Person and a method which can returns the fullName of person.
After that, we have created a constructor 'rohan' of 'Person' object and passed the values that we want to set as the properties of those constructor.
So this is the way of creating the constructors of an object.
I hope this post will help you to learn JavaScript More....
0 Comments
Please do not add Any Spam link in Comments !