JavaScript and Object-Oriented Programming

Abstract

Burns (2008) explained that while Java is an object programming language, JavaScript has smaller sets of commands that are exist in Java which makes the similarity between the two languages quite obvious. The important differences between Java and JavaScript are:

 

  • Java can be developed in a program that can be standalone, while JavaScript always need to be embedded within an HTML document to be executed on the fly via browsers.  
  • To run Java on the web (Java applets), Java program has to be compiled to the machine language, JavaScript runs through the browser where a built-in interpreter within the browser is used to execute the script.
  • It’s faster for the developers to create event handlers and commands in JavaScript than doing the same in Java since the debug environment in JavaScript is easier to be handled.

Deitel and Deitel (2008) stated that JavaScript uses objects to perform many tasks and therefore is referred to as an object-based programming language. Some of the built-in objects in JavaScript are:

 

  • Math Object – Allow developers to perform many common mathematical calculations.
  • String Object – It provides many methods that accomplish useful tasks such as selecting characters, combining strings, obtaining substrings of a string, and searching for substring within a string.
  • Date Object – Provides methods for date and time manipulations.
  • Document Object – Is used to manipulate the document that is currently visible in the browser window.
  • Window Object – Provides methods for manipulating browser windows.

Objects in JavaScript

Djajadinata (2007) explained that JavaScript uses object-oriented programming approach in its libraries to make the code more maintainable and manageable to the developers. In a normal object programming language such as Java and C++ objects can have different methods and properties. In JavaScript, objects represent collections of name/value pairs. The following is code snippet in how to create an object in JavaScript:

Var MyObject = new Object();

MyObject.MyTime = new Date();

alert(MyObject.MyTime);

Djajadinata (2007) explained that while in any object-oriented language there’s a difference between functions, and objects, in JavaScript such difference is fuzzy or indistinct. In JavaScript, function is considered an object that will be associated and executed within the code. The following is the code snippet that can explain the above statement:

Function MyFunc(x)

{

    alert(x);

}

MyFunc(“test”);

The same above example can be written as follow:

Var MyFunc = function(x) {

  alert(x);

};

MyFunc(“text”)

Djajadinata (2007) stated that another important fact about OOP in JavaScript is that, JavaScript has no classes like Java, C++ and C#, however, the only thing that can be built within the JavaScript that can close to the class is the constructor function. The following code snippet explains the constructor function:

Function MyConstructor(parm1)

{

   This.param1 = param1;

   This.reposntTo = function(param1)

    {

        If (this.param1 == param1)

        {

            alert(“test”);

         }

    };

 

}

var  myspot = new MyConstructor(“myspot”);

myspot.responseTo(“test2”);

myspot.reposneTo(“myspot”);

Embedded Objects with HTML

Conway and Oliver (2001) explained that objects can be embedded within HTML content via the object tag …. Using the Object element developer can include audio, videos, images, java applets, and flash animations within the HTML content. HTML 4.01 introduced such tag to be used instead of using the non-standard tag that was used in previous versions of HTML. Such tag provides a general purpose for many objects that can be included in HTML content. With such tag, everything required by the embedded object within the HTML content can be presented. Also, such tag can represent any sort of document within the HTML content.

Darie and Brinzarea (2007) explained that with the web 2.0 implementation; many objects can be embedded within the HTML content such as Synchronize JavaScript and XML that created a technology such as AJAX, and also the built-in objects controls that can be used when application is built via ASP.Net. Such implementation of objects within the HTML document, gives the web application a powerful tool where it becomes more flexible user interface, and reduces the time used to travel back and forth to the web server. Also, such techniques added more dynamic look to the web pages that can make users more active with the web site, and reduce the waiting time for things to happen online.

Evjen and Hanselman (2006) explained that many benefits can be added to the web development and developed web site on different browsers from using objects controls within the HTML content over the HTML built-in standard/traditional controls. For example, using ASP.Net object controls has many advantages over the standard built-in HTML controls when are used in web development. Some of these advantages are:

 

  • Detecting the target browser is huge benefit that can be added to the web development by adding ASP.Net controls instead of standard HTML controls.
  • Object controls runs on the server side where validation, and other process can run without the need of the scripting language that might has a compatibility problem.
  • Object controls have more properties and methods that add more flexibility in changing the outlook, and the behaviour of such controls compared to the traditional HTML control.
  • Object control has a good mechanism to identify the compatibility of the client browser; such capability is not exist in the traditional HTML controls.

Conclusion

Many people confuse the scripting language JavaScript with the programming language Java. Java is an object-oriented programming language used to develop applications on small-scale such as mobile devices to a large-scale distributed enterprise application and web application.

On the other hand, JavaScript is a browser-based scripting language developed to be implemented in all major browsers (Deitel and Deitel, 2008). JavaScript is powerful and flexible enough to accommodate the class-based programming style in many of the famous programming language such as Java and C++, however, writing the JavaScript in such style can make the code more complicated not easy for others to maintain. The future is holding more capabilities of using objects embedded within HTML document that can add more flexibility, and more dynamic features within the web pages.

References

Burns, J. (2008) Java vs. JavaScript [Online]. Available from: http://www.htmlgoodies.com/beyond/javascript/article.php/3470971 (Accessed: 19 February 2010).

Conway, S. & Oliver, S. (2001) HTML 4.01 Programmer’s Reference. 2nd ed. Birmingham: Wrox Press.

Deitel, P. & Deitel H. (2008) Internet & World Wide Web: How to Program. 4th ed. New Jersey: Pearson Prentice Hall.

Djajadinata, R. (2007) JavaScript- Create advanced Web Applications With Object-Oriented Techniques [Online]. Available from: http://msdn.microsoft.com/en-us/magazine/cc163419.aspx (Accessed: 19 February 2010).

Darie, C. & Brinzarea, B. (2007) Object-Oriented JavaScript: Part 3 [Online]. Available from: http://www.webreference.com/programming/javascript/object-oriented_javascript3/ (Accessed: 19 February 2010).

Evjen, B. & Hanselman, S. (2006) Professional ASP.Net 2.0 Special Edition. 2nd ed. Indianapolis: Wrox Press.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: