Welcome to the “Advanced JavaScript” article series. As the name suggests, we are talking about a few object oriented features of JavaScript. We have covered many more important topics; you can read them by visiting the following links.
You may find another article in this series that is exculsively written about objects in the JavaScript language and later I learnd that a few more important features need to be explained in the same context. This is the reason for writing another article that shows a few more interesting facts of JavaScript objects. Earlier articles have explained objects in JavaScript so there is no need to repeat that explanation. We will reach our target topic directly. How to use JavaScript objects like a Dictionary in C#. (Is there a Dictionary concept in any other language? I don’t know.) That’s fine so let’s implement our first example.
Oh, do you want a small explanation? Ok, a JavaScript Object is very similar to a Dictionary<string,Object> in the sense that we can associate any arbitrary piece of data (any object) to any arbitrary string. If you are familiar with JSON notation then you know how to access objects using the dot (.) operator but the beauty of a JavaScript object is that we can access a property like a dictionary using the [] operator. Here is a sample representation of our explanation.
JavaScript Objects are Dictionaries
Please don’t consider jQuery functions (document.ready) in this example, we have done it to make life easy. (Ha..Ha..) We have declared a “name” object in this example and initialized a property of the name object using the [] operator. The [] operator is taking one string as the key and the value as data.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs"
Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="Jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<script>
$(document).ready(function () {
var name = new Object();
name["firstName"] = "Sourav";
name["lastName"] = "Kayal";
console.log(name["firstName"] + name["lastName"]);
});
</script>
</form>
</body>
</html>
And please note that we are accessing the properties of the name object in the same way, using the [] operator. Is it not like a Dictionary access in C# ? Here is the output of this example.
Is it possible to update a Dictionary (read object) after creation?Yes, it’s possible to update an object after creation. In this example we will update the same object that we created in our previous example. Have a look at the following code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs"
Inherits="JavaScript.JavaScript" %>
<head runat="server">
<script type="text/javascript" src="Jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<script>
$(document).ready(function () {
var name = new Object();
name["firstName"] = "Sourav";
name["lastName"] = "Kayal";
name["showName"] = function () {
console.log(name["firstName"] + name["lastName"]);
};
//Update name here
name["firstName"] = "Manish";
name["lastName"] = "Khanra";
name.showName();
});
</script>
</form>
</body>
</html>
Here is the output and we see that the value has been updated with a new value that we set after object creation.
Is it possible to set an anonymous function to an object property?You might think, what is this silly question? By using the dot (.) operator we can set an anonymous method in an object’s property so why not in this way? Yes, it’s just similar to the dot (.) operator to attach an anonymous function to a property . In this case, instead of the dot (.) we will use the [] operator. Here is a sample implementation.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs"
Inherits="JavaScript.JavaScript" %>
<head runat="server">
<script type="text/javascript" src="backbone/Jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<script>
$(document).ready(function () {
var name = new Object();
name["firstName"] = "Sourav";
name["lastName"] = "Kayal";
name["showName"] = function () {
console.log('Name is :-'+ name["firstName"] + name["lastName"]);
};
name.showName();
});
</script>
</form>
</body>
</html>
This is the output.
Add an object property after definitionIt’s also possible to add an object property after definition. The reason is JavaScript is a class-less programming language, in other words there is no proper template of object creation. (Whereas it is strongly present in C# and Java and other languages). So where there is no proper template of an object, why is it necessary to create a property on the fly? Here is some code for you.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs"
Inherits="JavaScript.JavaScript" %>
<head runat="server">
<script type="text/javascript" src="backbone/Jquery.js"></script>
</head>
<body>
<form id="form1" runat="server">
<script>
$(document).ready(function () {
var name = new Object
{
name["firstName"] = "Sourav";
name["lastName"] = "Kayal";
name["showName"] = function () {
console.log(name["firstName"] + ' '+ name["miffleName"] + ' ' + name["lastName"]);
};
}
//Update name here
name["miffleName"] = "Kumar";
name.showName();
});
</script>
</form>
</body>
</html>
In this example the middle name is a property that has been appended after object creation and it’s working fine.
Conclusion
I consider myself a very beginner in JavaScript (and also in other languages) and learning day to day. When I saw those new features I could not stop myself from writing about it for others. I promise, if I see anything wonderful like this, I will write about it for you. Bye..Happy day.