What is an objects in Javascript

What is an object in Javascript?

If you have read my last post about object-oriented programming in Javascript. I hope you have got some idea about what is object-oriented programming in Javascript. so Now we will see What is an object in Javascript.

 First, we will look at what is meant by object:

An object is a real-time object and can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier.

Let's take a look at the following code.
var userName1="Vicky";
var userPassword1="VickyPassword"
var userEmail1= "vicky@vicky.com"

var userName2="Sagar";
var userPassword2="SagarPassword"
var userEmail2= "sagar@test.com"

var userName3="John";
var userPassword3="jOhnPassword"
var userEmail3= "easyjohn@sample.com"

As you can see if we want to use more than 100 records then it will become a tedious job to handle it properly, instead of that if we use objects then the code will look like as follows:
function User(name, password, email) {
    this.name = name;
    this.password = password;
    this.email = email;

user1 = new User("Vicky", "VickyPassword", "vicky@vicky.com")
user2 = new User("Sagar", "SagarPassword", "sagar@test.com")
user3 = new User("John", "jOhnPassword", "easyjohn@sample.com")

 And its output will be as follows:

What is an Javascript objects

which is better to write and maintain code? the second one right?
Now we will understand what exactly we did and we will try to understand some OOPS concepts with the above example.

The function User() is class in OOPS where name and password etc are his properties.
and by using the new User()  we are creating an object of that class.

So I hope you understand the meaning of Object. So let's understand more about it.

An object will have its own properties and method:

if we take an example of a car then User have properties like user.name, user.password, user.emailid etc and user.login(), user.isLoggedIn() are some methods.

Almost everything in Javascript is Object:

If we see properly javascript itself uses objects. For example,  
  • Any datatype can be objects (if defined with the new keyword)
  • Strings can be objects (if defined with the new keyword)
  • Dates, Maths, Regular Expressions, Array, Functions are always objects.

Creating Object in Javascript:

There are multiple ways to generate objects:

Using an object literal.

It's the easiest way to create an object in javascript, by using it you can define & create an object in a statement.

var user = {name:"Vicky", password:"VickyPassword", email:"vicky@vicky.com"}

With the keyword new.

You can use new Object() to create an object. 

var user = new Object();
user.name = "Vicky"
user.password = "VickyPassword"
user.email = "vicky@vicky.com"

That's it I hope you understand what is an object in javascript, Please comment if you want any clarification on any point or something.


Javascript Objects are mutable:

It means objects are addressed by reference, not by value.

var user1 = User;

this will not create a new object, but it will share the same object as User.

That's it, for now, please comment if you have some doubts on it.

What is an objects in Javascript What is an objects in Javascript Reviewed by Vikas Kad on Thursday, September 20, 2018 Rating: 5

1 comment:

  1. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    WordPress website development Chennai


Powered by Blogger.