Introduction
In previous articles we learned about the basics of JavaScript and JavaScript functions. In this article we will learn about the basics of creating and manipulating objects in JavaScript.
Creating Object
-
Literal Based Object
This is the most frequent and popular way to create objects in JavaScript. If you are not requiring multiple instances of an object then Literal Based object is based on the idea. This type of object is denoted by curly braces. e.g.- < script type = "text/javascript" >
- var book = {
- name: "Getting Started With JavaScript",
- author: [“KP”, ”David”],
- getName: function () {
- return this.name
- }
- }; <
- /script>
In the above example we created one object named “book”. Inside the curly braces the properties (we’ll discuss it later) and its value are defined as a collection of key and value pairs.Here the key is identifiers or strings while the value can be any valid expression which means it may be string, number, object, function, array, etc. In the object, the collections of properties (key/value pair) is comma(,) delimited and each property name and its value are separated by a colon(:). -
Using a constructor function
We can create a JavaScript object using the constructor as well by using these steps:a. First we have to define an object type by writing a constructor function.
b. Second Create an instance of the object by using the new keyword.Now, let's create a constructor function for the object type that contains its name, properties and methods. e.g.- < script type = "text/javascript" >
- function Book(name, author, year) {
- this.name = make;
- this.author = model;
- this.year = year;
- } <
- /script>
In above example you see that we use this keyword to assign value to properties of its object. Note that this is a reserve keyword by JavaScript.Ok, now we have to create an object. e.g.- var myBook = new Book(‘Getting Started With JavaScript’, ’KP Singh’, ’2016’);
- //We can also create multiple objects e.g.
- var javaBook = new Book(‘Getting Started With Java, ’KP Singh’, ’2016’);
- var csharpBook = new Book(‘Getting Started With C#, ’Robin Singh’, ’2016’);
- var phpBook = new Book(‘Getting Started With PHP, ’Priya Singh’, ’2016’);
Using the Object. create method
We can also create an object by using Object.create. It is most useful when we want to inherit one object directly from another object e.g.In the above example you can see that first I create one object named “girl” with two properties:- name and traits. And after that I used Object. create a () function to create the instance of girl, where the girl will be a prototype (We'll discuss it in the upcoming article).- < script type = "text/javascript" >
- var girl = {
- name: ””,
- traits: {}
- };
- //Create the instance of girl object
- var deepika = Object.create(girl);
- deepika.Name = ”Deepika”;
- deepika.traits.age = 35,
- deepika.traits.weight = 70,
- deepika.traits.favColor = ”Blue”
- console.log(“Deepika”, deepika.traits) // Output : Deepika: Object { age=35, weight=70, favColor=”Blue” }
- <
- /script>
-
Object and Properties
Basically JavaScript object properties are associate with it and defined the characteristics of objects. We can access JavaScript Object properties by using dot (.) notation like another server-side programming language e.g.We can access JavaScript Object Properties value and also assign value according to our requirements. As we know that JavaScript is case sensitive, hence its objects and properties are also case sensitive. e.g.- Myobject.propertyname
- Myobject. propertyname
- Myobject.Propertyname
In the above simple example both properties are different.
Conclusion
Read more articles on JavaScript

SubashPosted Aug 30, 2016, 12:49 AM
Nice
Rahul Pushpendu BhaskarPosted Jun 30, 2016, 7:10 AM
Waiting for next.....
Rahul Pushpendu BhaskarPosted Jun 30, 2016, 7:09 AM
Good one.. Please go ahead :-)
Rahul Kumar SaxenaPosted Apr 8, 2016, 7:42 AM
Good Show
Jyotil RavalPosted Apr 8, 2016, 3:15 AM
Thanks a lot man, I really appreciate this for giving lessons in such a easy language.
Surjeet BhadairiyaPosted Apr 8, 2016, 2:59 AM
Very Helpful... it's very easy to understand easily. Thank You so much for sharing this.