How to access declared objects in javascript ( object instances )

  • 0

Object instance need to access properties and methods of an object.

When we used constructor method to define objects, we need to create an instance of the object to use the properties and methods of that object.

function myobj(){
this.name= “joe”;  //Property
this.age= “20”;   //Property
this.nameandage= function(){   // Method
console.log(this.name+’with age’+this.age);
}
}

        To create an instance of this object :
var insta_myobj= new myobj(); // creating instance using “new” keyword
insta_myobj.nameandage(); // accessing Method

 

In literal object definition:

No need to create a specific instance of this object call the properties and methods.

var myobj={
fname:”joe”,//Property
lname:”john”,//Property
fullname: function (){
console.log(this.fname +” ” + this.lname);
}
}

we can call these properties and functions as:

myobj.fname;

myobj.fname=”jacob”;

myobj.fullname();

Object instance need to access properties and methods of an object. When we used constructor method to define objects, we need to create an instance of the object to use the properties and methods of that object. function myobj(){ this.name= “joe”;  //Property this.age= “20”;   //Property this.nameandage= function(){   // Method console.log(this.name+’with age’+this.age); } }       …

Object instance need to access properties and methods of an object. When we used constructor method to define objects, we need to create an instance of the object to use the properties and methods of that object. function myobj(){ this.name= “joe”;  //Property this.age= “20”;   //Property this.nameandage= function(){   // Method console.log(this.name+’with age’+this.age); } }       …