{} it's an object in the JavaScript | standard ES5
last updated: 1 Augusta 2020
Object in the JavaScript it is a set of properties
Example: The workbook has properties
• name
• author's name
• price
• Pages
Create a populated object
To create an object you need to write
var object name =
{
property 1 : value 1,
"property 2" : value 2, // you can write the property in quotation marks
...
};
Html
Example
<html>
<body>
<script>
// create an object
var book1 =
{
// property set
Name: "Wizard of the Mediterranean" ,
Price: 120
};
// on the screen we will see "Wizard of the Mediterranean"
alert (book1.Name);
// create an object
var book2 =
{
// property set
"Name" : "Lord of the Rings" ,
"Price" : 60
};
// on the screen we will see "Lord of the Rings"
alert (book2.Name);
</script>
</body>
</html>
Create an empty object
An empty object can be created in 3 ways:
Method 2
var book1 = new Object();
Method 3
var book1 = Object.create( Object.prototype );
To add a property to an object
Method 1
book1.Name = "Wizard of the Mediterranean";
Method 2
book1["Name"] = "Wizard of the Mediterranean";
Html
Example
<html>
<body>
<script>
// Method 1. Create an empty object
var book1 = {};
// Add a property
book1.Name = "Wizard of the Mediterranean" ; // method 1
book1["Author" ] = "Ursula Le Guin" ; // method 2
// on the screen we will see "Wizard of the Mediterranean Ursula Le Guin"
alert (book1.Name + " " + book1.Author);
// Option 2. Create an empty object
var book2 = new Object();
// add a property
book2.Name = "Lord of the Rings" ; // method 1
book2["Author" ] = "Nick Perumov" ; // method 2
// on the screen we will see "The Lord of the Rings Nick Perumov"
alert (book2.Name + " " + book2.Author);
</script>
</body>
</html>
Take, get a property from an object
Method 1
var value1 = book1. Name;
Method 2
var value1 = book1[ " Name" ] ;
Html
Example
<html>
<body>
<script>
// create an object
var book =
{
// property set
Name: "Wizard of the Mediterranean" ,
Author : "Ursula Le Guin" ,
};
// get the property of the object (method 1)
var value1 = book.Name;
// value1 = "Wizard of the Mediterranean";
// get the property of the object (option 2)
var value2 = book['Name'];
// value2 = "Wizard of the Mediterranean";
</script>
</body>
</html>
Select (enumerate) all elements in the object
Html
Example
<html>
<body>
<script>
// create an object
var book =
{
// property set
Name: "Wizard of the Mediterranean" ,
Author : "Ursula Le Guin" ,
};
// on the screen we will see
for ( key in book )
{
// iteration 1
// key = "Name"
// book[key] = "Wizard of the Mediterranean"
// iteration 2
// key = "Author"
// book[key] = "Ursula Le Guin"
}
</script>
</body>
</html>
Create a complex object
Html
Example
<html>
<body>
<script>
// create an object
var book =
{
// property set
Name: "Wizard of the Mediterranean" ,
Author : "Ursula Le Guin" ,
// nested set of properties
OtherInfo :
{
Price: 120,
PagesCount: 90
}
};
// on the screen we will see 120
alert (book.OtherInfo.Price);
</script>
</body>
</html>
Delete an object
delete book.Name;
Comparison on empty
if (book.Name == undefined )
alert("empty");
if (book.Name != undefined )
alert("not empty");
Html
Example
<html>
<body>
<script>
// create an object
var book =
{
// property set
Name: "Wizard of the Mediterranean" ,
Author : "Ursula Le Guin" ,
};
// on the screen we will see 'not empty'
if (book.Name != undefined)
alert ("not empty" );
// delete the object
delete book.Name;
// on the screen we will see 'empty'
if (book.Name == undefined)
alert ("empty" );
</script>
</body>
</html>
An object can contain functions
Html
Example
<html>
<body>
<script>
// create an object
var book =
{
// property set
Name: "Wizard of the Mediterranean" ,
Author : "Ursula Le Guin" ,
// nested set of properties
OtherInfo :
{
Price: 120,
PagesCount: 90
},
// function
myShow : function(title)
{
alert (title + ' ' + this .Name + ' ' + this .OtherInfo.Price);
}
};
// calling a function on an object
book.myShow('Hello');
// on the screen we will see 'Hello Wizard of the Mediterranean 120'
</script>
</body>
</html>