Introduction
This is the second presentation of the Advanced JavaScript article series. As the name indicates, this series provides somewhat deeper concepts of JavaScript. The previous article provided the history of JavaScript and the importance of JavaScript in modern web applications. You can read it here.
- Advance JavaScript: History and role of JavaScript behind modern web
- Advance JavaScript: play with object in JavaScript
- Advance JavaScript: Function Definition Style in JavaScript
- Advance JavaScript: Understand undefined in JavaScript
- Advance JavaScript: Understand "class"-ical concept of JavaScript
- Advance JavaScript: Implement inheritance in JavaScript
- Advance JavaScript: Callback design pattern and callback function in JavaScript
- Advance JavaScript: Exception handling in JavaScript
- Advance JavaScript: Scope of Variable in JavaScript
- Advance JavaScript: closure in JavaScript
Advance JavaScript
Objects in JavaScript
- Dates are objects in JavaScript
- Arrays and strings are always objects
- Math and regular expressions are always objects
- The most interesting fact is that functions are also objects in JavaScript
Ok, so the concept is that everything (almost) in JavaScript is an object or at least they behave like an object. We all now have the concept of objects in Object-Oriented Programming. They have properties and methods. The syntax to access a property of an object is:
objectName.propertyName
The string is an object.
Let's create a simple object. As we have explained earlier, a string is also an object in JavaScript. We will use one example of a string.
- <head id="Head1" runat="server">
- <title></title>
- <script>
- function TestScript() {
- var val = "JavaScript";
- alert("Length of String is:- " + val.length);
- }
- </script>
- </head>
- <body onload="TestScript();">
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>

Few more objects in JavaScript
- <script>
- function TestScript() {
- //Create Date Object
- var date = new Date();
- //Create Array Object
- var array = new Array("Sourav", "Kayal");
- //Math object has random() function
- var data = Math.random();
- }
- </script>
Create object and set property on the fly
In this example we will create an object on the fly and we will set few properties on that. This object creation is very similar to object creation in high-level programming languages. The difference is that it does not require a class definition.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <script>
- function TestScript() {
- var author = new Object();
- author.name = "Sourav Kayal";
- author.interest = "JavaScript";
- author.publisher = "cshrpcorner.com";
- alert("Name :- " + author.name + "Interest:- " + author.interest + "Publisher:- " + author.publisher);
- }
- </script>
- </head>
- <body onload="TestScript();">
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>

Set property of object using the constrictor
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <script>
- function person(name, surname) {
- this.name = name;
- this.surname = surname;
- }
- function TestScript() {
- var p = new person("Sourav", "Kayal");
- alert("Name:- " + p.name + " Surname:- " + p.surname);
- }
- </script>
- </head>
- <body onload="TestScript();">
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>

- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <script>
- function Addition(num1, num2) {
- this.num1 = num1;
- this.num2 = num2;
- alert(num1 + num2);
- }
- function TestScript() {
- var value = new Addition(10, 20);
- }
- </script>
- </head>
- <body onload="TestScript();">
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
Output
This is another style to define a function and the result is absolutely the same in both (the above style and this style).

Another style to define a function.
- <script>
- var Addition = function (num1, num2) {
- this.num1 = num1;
- this.num2 = num2;
- alert(eval(num1) + eval(num2));
- }
- function TestScript() {
- var value = new Addition(10, 20);
- }
- </script>
An obvious question in people's minds is, what is the difference between the two syntaxes? Let's try to understand them.
- function hello() {
- }
- var hello = function () {
- }
Property of the object is also an object
Let's see how to attach a function object to the property of another object. Here a person is one object and in the next line, we are assigning an anonymous function to the name property of the person object. Hence the name property of the person object is behaving as a function object.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <script>
- var person = {};
- person.name = function (name1) {
- alert("I am " + name1);
- }
- function TestScript() {
- person.name("sourav kayal");
- }
- </script>
- </head>
- <body onload="TestScript();">
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>

Conclusion
Previous article: Advance JavaScript: History and Role of JavaScript Behind Modern Web

Mahesh AllePosted Nov 7, 2013, 1:09 AM
Nice explanation and good job. Thanks for posting this one. I have one question, in you code alert(eval(num1) eval(num2)); what is mean by eval and how this will work. Can you tell,