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

By the end of this tutorial you will be aware of:
- What are Javascript Objects?
- How to Declare Javascript Objects?
- How to give Properties or Methods to an Object?
- How to Access Object Pair?
- How to Remove Object Pair?
- Example to use Javascript Objects
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
Views:
1599
0 Comment
Guest
Recommend
0
Sort by Newest
Be the first to say something...
Related Articles
- How to Secure your JavaScript Code from being Copied by Competitors | Javascript Tutorial
- How to Promote JavaScript To Speed Your Website Loading Time For Higher SEO Performance | Javascript Tutorial
- Best Way To Loop Through Javascript Array Of Objects | Javascript Tutorial
- Best Way To Create Javascript Array Of Objects | Javascript Tutorial
- Best Way To Loop Through Javascript Array | Javascript Tutorial
- How to Create Javascript Multidimensional Array | Javascript Tutorial
Latest Articles
- What are Php Arithmetic and Comparison Operators ? | Php Tutorial
- What are Php Variables and Types ? | Php Tutorial
- How to write your first php web page with code example | Php Tutorial
- Introduction to Php Learning Course | Php Tutorial
- How to Secure your JavaScript Code from being Copied by Competitors | Javascript Tutorial
- How to Promote JavaScript To Speed Your Website Loading Time For Higher SEO Performance | Javascript Tutorial