|
||||||
Choosing an OOP Language for Web BrowsersComparing VBScript and Javascript Object Oriented ProgrammingObject 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:
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 ClassEvery class in every programming language that supports object oriented programming has the same basic construct. The class definition contain:
And, of course, the programmer will need some means of instantiating an object from the class. Creating a Class in JavascriptAt 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 VBScriptThe 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.
|
||||||
|
|
||||||
|
|
||||||