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:
And this class is instantiated by using the "new" method:
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:
And the VBScript class even has a constructor method:
It is instantiated and used in the same way as the Javascript class, and that's by using the "new method"
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.