Choosing an OOP Language for Web Browsers

Comparing VBScript and Javascript Object Oriented Programming

© Mark Alexander Bain

Sep 25, 2009
Choosing an OOP Language for Web Browsers, Mark Alexander Bain
Object oriented programming is an important concept for any programmer. However, the new programmer needs to consider which is the better language to learn on.

There are pros and cons to using each and every programming language, and so anyone new to creating PC applications and wanting to understand concepts such as object oriented design has a problem.

Which programming language should they start off with? And there are, as always, compromises that have to be made. For example, for someone wishing to start creating games for web pages will have to consider facts such as:

  • Java is very powerful, but can be difficult to understand (especially for the new programmer). It also requires additional, third party software to be installed to compile programs and to see the results in a web web browser
  • Javascript code, like Java, can be confusing and quite difficult to understand, but it will run in any web browser and the programmer only requires a simple text editor in order to create an application
  • VBScript (or Visual Basic script) is easy to understand, its structure is very close to every day English and, like Javascript, only needs a standard text editor. However, the code will only run in a Microsoft Internet Explorer web browser, and even that worries about running VBScript applications (as can be seen in figure 1 at the bottom of this article)

The problem is not that there is a problem with VBScript as such. It's just that VBScript is actually very powerful and, in the wrong hands, can do a lot of damage to a computer. Javascript can't do all of the things that VBScript can do and so there are not the same potential security issues.

However, in the right environment, such as at home or on a company intranet where the programmers are known and trusted, then VBScript can be used to create useful tools and as an excellent training environment.

To Illustrate these points it is, therefore, worth considering how the same tasks are done in both Javascript and VBScript, and a good example is the creation of a custom class.

The Structure of a Class

Every class in every programming language that supports object oriented programming has the same basic construct. The class definition contain:

  • properties
  • methods

And, of course, the programmer will need some means of instantiating an object from the class.

Creating a Class in Javascript

At first glance a Javascript class looks like a function. However this apparent function can contain properties and methods:

<script language=javascript>
function jAlien () {
this.image = new Image();
this.image.src = "alien.png";
this.image.style.position = "absolute";
this.show = function(left, top) {
this.image.style.top = top;
this.image.style.left = left;
document.body.appendChild(this.image);
}
}

And this class is instantiated by using the "new" method:

var alien1 = new jAlien ();
alien1.show(100,140);
</script>

If this code is added to a .html file and viewed in any web browser then the new object will be visible.

Creating a Class in VBScript

The class definition in VBScript does the same job as the class definition in Javascript, but this time it is much clearer what is going on:

<script language="vbscript">
Class vbAlien
Public image

And the VBScript class even has a constructor method:

Private Sub Class_Initialize
Set image = document.createElement("img")
image.src = "alien.png"
image.style.position = "absolute"
End Sub
Public Sub show (left, top)
image.style.top = top
image.style.left = left
document.body.appendChild(image)
End Sub
End Class

It is instantiated and used in the same way as the Javascript class, and that's by using the "new method"

Dim alien2: Set alien2 = new vbAlien
alien2.show 250,200
</script>

And again, if this is viewed via a web browser the the image incorporated into the class will be visible . However, this time it can only be seen in Microsoft Internet Explorer (as shown in figure 2). It will not be visible in other web browsers such as Firefox (as shown in figure 3).

This leaves the programmer with a choice. They can either accept the browser limitations in order to use the VBScript class structure, or use the Javascript class structure in the knowledge that it will work in every web browser regardless of who created it.


The copyright of the article Choosing an OOP Language for Web Browsers in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Choosing an OOP Language for Web Browsers in print or online must be granted by the author in writing.


Choosing an OOP Language for Web Browsers, Mark Alexander Bain
Figure 1: Paranoid Internet Explorer, Mark Alexander Bain
Figure 2: VBScript Classes, Mark Alexander Bain
Figure 3: Firefox Won't Use VBScript, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo