WBxs
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

Adam Hassan

Adam 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: 196
× Close
Results:
Want to Enable Key Features? Create a Free Account.