JavaScript Solutions, Competitive programming in JavaScript, MCQ in JS

Thursday, 20 September 2018

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= ""

var userName2="Sagar";
var userPassword2="SagarPassword"
var userEmail2= ""

var userName3="John";
var userPassword3="jOhnPassword"
var userEmail3= ""

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) { = name;
    this.password = password; = email;

user1 = new User("Vicky", "VickyPassword", "")
user2 = new User("Sagar", "SagarPassword", "")
user3 = new User("John", "jOhnPassword", "")

 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.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:""}

With the keyword new.

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

var user = new Object(); = "Vicky"
user.password = "VickyPassword" = ""

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.

No comments:

Post a Comment