Javascript Object ?

An object is a representation of a “thing” (someone or something), and this representation is
expressed with the help of a programming language. The thing can be anything—a
real-life object. Object like a cat for example, you can see that it has certain characteristics (color, name, weight) and can perform some actions (meow, sleep, hide, escape). The characteristics of the object are called properties in OOP and the actions are called methods.

The simplest way to create a custom object is to create a new instance of object and add properties and methods to it, as in this example:

var person = new Object(); 
  person.name = “Dileep”; 
  person.age = 25; 

 person.job = “Software Engineer”; 
 person.sayName = function(){ 
 alert(this.name); 
}; 

This example creates an object called person that has three properties ( name , age , and job ) and one method ( sayName() ). The sayName() method displays the value of this.name , which resolves to person.name . Now,we are using this pattern frequently to create new objects.

Another way to define object is through object literal notation as follows:

var person = { 
    name: “Dileep”, 
    age: 25, 
    job: “Software Engineer”, 
    sayName: function(){ 
       alert(this.name); 
       } 
 }; 

Both are the ways to define the object using the same properties and the same method.But now a days people are preferring to go with Object literals pattern for creating objects.

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Javascript Tagged with: ,