Objects are just data, with added properties and methods.
Object Properties and MethodsProperties are values associated with objects.A Real Life Example.
In real life, a car is an object. It has properties like weight and color, and methods like start and stop:
All cars have the same properties, but the property values differ from car to car.All cars have the same methods, but they are performed at different times.JavaScript Objects
In JavaScript, objects are data variables with properties and methods. Almost "everything" in JavaScript are treated as objects. Dates, Arrays, Strings, Functions. In JavaScript you can also create your own objects. This example creates an object called "person" and adds four properties to it:
Example:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var person = {firstName:"John", lastName:"Elwin", age:22, eyeColor:"blue"}; document.getElementById("demo").innerHTML = person.firstName + " is " + person.age + " years old."; </script> </body> </html>Spaces and line breaks are not important.An object declaration can span multiple lines:
Example:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var person = { firstName : "John", lastName : "Elwin", age : 22, eyeColor : "blue" }; document.getElementById("demo").innerHTML = person.firstName + " is " + person.age + " years old."; </script> </body> </html>There are many different ways to create new JavaScript objects. You can also add new properties and methods to already existing objects.Accessing Object Properties
You can access the object properties in two ways:
Example:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var person = { firstName: "John", lastName : "Elwin", id : 45**** }; document.getElementById("demo").innerHTML = person.firstName + " " + person["lastName"]; </script> </body> </html>Accessing Object Methods
You can call an object method with the following syntax: objectName.methodName()
This example uses the fullName() method of a person object, to get the full name:
Example:
<!DOCTYPE html> <html> <body> <p>Creating and using an object method:</p> <p id="demo"></p> <script> var person = { firstName: "John", lastName : "Elwin", id : 45****, fullName : function (){return this.firstName + " " + this.lastName} }; document.getElementById("demo").innerHTML = person.fullName(); </script> </body> </html>Object methods are ordinary JavaScript functions defined as object properties.Associative arrays in PHP. Hash tables, hash maps or hashes in C, C++, C#, Java, Perl, and Ruby. Dictionaries in Python.