Mohamed Sobhy
Feb 12, 2018

Welcome to our tutorial, now I am going to explain how to create javascript array of objects with examples.

protect your javascript code

By the end of this tutorial you will be aware of:

What are Javascript Objects?

Javascript objects are containers that allow to use names to access its properties (name:value pair) or methods (name:function pair).

How to Declare Javascript Objects?

There are mainly two ways to declare an object.
// First, to directly use variable types as below:

var aa = new Object(); // Declares aa as a Variant object

var bb = new String(); // Declares bb as a String object

var cc = new Number(); // Declares cc as a Number object

var dd = new Boolean(); // Declares dd as a Boolean object
// Second, to use a constructor function as below:

function AnyName(propA, propB, propC) {
this.propA = propA;
this.propB = propB;
this.propC = propC;
}
var ee = new AnyName(); // Declares ee using a constructor

How to give Properties or Methods to an Object?

You can give a property to an Object in form of name:value pair, while a method can be given in form of name:function pair as shown.
var person = {
firstName:"Adam", //property

lastName:"Hassan", //property

fullName: function() {return this.firstName + " " + this.lastName;} //method

};

How to Access Object Pair?

You can access Object property in two ways, while Object method in one way as follow:
var person = {firstName:"Adam", lastName:"Hassan"};
document.write(person.firstName); //Access Object Property

document.write("<BR>");
document.write(person["firstName"]); //Access Object Property

//Where, person.firstName or person["firstName"] returns Adam

document.write("<BR>");
document.write(person.fullName()); //Access Object Method

//Where, person.fullName() returns Adam Hassan

How to Remove Object Pair?

In order to remove a property or method, just call "delete name" as below:
var workers = {admin:"Jack", accountantA:"John", accountantB:"Mark"}
delete workers.accountantB;

Example to use Javascript Objects

Next example shows how to use objects:
<script>
function Sport(Name, Team, Score) {
this.Name = "Football",
this.Team = "Victory",
this.Score = "1st",
this.By = "Adam",
this.Info = function() {return "Reporter: " + this.By;}
}
var Sport1 = new Sport();
document.write("In " + Sport1.Name + ", " + Sport1.Team + " team was the " + Sport1.Score + " in the champions. " + this.Info);
</script>

Try WhiteBoxes JavaScript Obfuscator >>

Thank you for reading the above article. Please let us know your comments below.

References:
  • Practical Experience
About the author

Mohamed Sobhy

Mohamed is a freelance web designer, recreational software developer, author and CEO of infoapper.com . Also, has passion to learn and transfer the know-how of every valuable thing.
Views: 1599
0 Comment Guest
Recommend 0
Sort by Newest
Be the first to say something...

Ask Community

Ask questions and Share knowledge with Community

Find below recent posts for automation solutions with questions and answers by community. You can search in past threads or post new question about your assignment with detailed description, and always could mark your question as request. Sharing knowledge are highly appreciated by answering on others questions, and in return awards will be decided.

× Close
Results: