Like any other object oriented programming language we can create object in JavaScript programming too, though JavaScript don’t support all four pillars of OOPs, but still we can do many things with JavaScript object.
In this tutorial you will learn how to create JavaScript object, JavaScript object constructor, JavaScript object to array, object properties etc
Creating JavaScript Object and using them in code is actually very organized way of writing JavaScript, it’s kind of clean code practice. you can create JavaScript object to string, and convert a javascript object to json.
Here we learn how many ways we can create a JavaScript object and use them with function or any other way.
<script> var student = new Object(); </script>
Objects in JavaScript are just collections of key-value pairs. The values can have any number of methods and properties, and may contain different JavaScript data types, like numbers, strings, date, Boolean etc.
We can create object in JavaScript in three different ways.
Here in example I am creating a student object with few properties, then writing them on webpage
<script> var student = new Object(); student.Name = "Abantika"; student.addess = "Kormongla, Bangalore"; student.subject = "Javascript"; document.write(student.Name + " of " + student.addess ); </script>
Here we see how we can set property values at the time of creating a new instance of an student object
<script> function getStudent(name, addess, subject) { this.name = name; this.addess = addess; this.subject = subject; } var _student = new getStudent("Ajit", "EM Pass, Kolkata", "JSON"); document.write(_student.name + " of " + _student.addess ); </script>
Object literal is like storing values with Key-value pair , this will be very useful when we learn JSON
<script> _stu = {name: "Deepika", address: "Bangalore", subject: "JavaScript"} document.write(_stu.name + " " + _stu.address + " " + _stu.subject); </script>
Object.keys()
returns enumerable properties, here is the syntax for Object.keys()
Object.keys(obj);
We just need to pass an object in keys method
let student = { Name : "Abantika", addess : "Kormongla, Bangalore", subject : "Javascript" } Object.keys(student);
If you want to get the length of an Object in JavaScript, for example we want to get the length of above student object
Object.keys(student).length
We can convert a JavaScript Object to JSON object by using Stringify method
studentObject = { Name : "Abantika", addess : "Kormongla, Bangalore", subject : "Javascript" }Use JavaScript function JSON.stringify() to convert to a string.
var myJSONobj = JSON.stringify(studentObject);
Here is another example of creating JavaScript object using function keyword
var Employee = function (name, city, salary) { this.name = name || ""; //Public attribute default value is null this.city = city || ""; this.salary = salary || 1000; }; // Create Employee class object var emp1 = new Employee("Aratri","Kolkata",3000); // Create Employee class object var emp2 = new Employee("Bristi","Bangalore",3500);
Above way of creating JavaScript object may be more interesting, because while creating a new instance of the object, you may feel like passing parameter values to constructor like we do in any other object oriented programming.