JScript .NET, Part II: Major Features: The Class Concept - Doc JavaScript
JScript .NET, Part II: Major Features
The Class Concept
Supporting the class concept is a must for modern programming languages. JScript .NET's support of classes is classic. A class acts as a template. You can create many objects with the template, all of them will be similar to the original class. When you instantiate a class, you create an object based on the class. If your class describes a commercial airplane in an airline's fleet, for example, you can instantiate a 747
with it, a 767
, or a 777
. You use the same template to create different airplanes. You construct different airplanes by giving them different traits during their instantiation.
A class includes properties and functions. They are the members of the class. A class's functions are similar to regular JavaScript functions. A class's functions are affiliated only with their class. They are the class's member functions. One of the member functions is very special--it is the constructor function. Here is an example class that constructs airplanes:
class Airplane { var model: int; function Airplane() { model = 767; } }
A class's constructor function has the same name as its class. All airplanes instantiated using the Airplane
class will be 767
:
var firstJet : Airplane; var secondJet : Airplane; firstJet = new Airplane(); secondJet = new Airplane();
Both jets in the listing are 767
because there's only one constructor that initializes the jet's model to 767
. A class can have more than one constructor, as shown in the following listing:
class Airplane { var model: int; function Airplane() { model = 767; } function Airplane(newModel : int) { model = newModel; } }
You can create a 767
using the class's default constructor:
var thirdJet : Airplane; thirdJet = new Airplane();
You can also create a 777
by calling the second constructor (with the parameter):
var fourthJet : Airplane; fourthJet = new Airplane(777);
You can expose a class's property simply by creating a member variable as above, and allowing other code to directly manipulate it:
class Airplane { var model: int; function Airplane() { model = 767; } } var fifthJet : Airplane; fifthJet = new Airplane(); fifthJet.model = 747;
This approach works fine, but it is not recommended to expose a class's variables to foreign code. It's better to create accessor functions. These are member functions that expose a class's properties as if they are member variables. These member functions are the set
and get
functions:
class Airplane { var model: int; function Airplane() { model = 767; } function get JetModel() : int { return model; } function set JetModel(newModel : int) { model = newModel; } }
You set and get the jet's model with the JetModel
member function. Notice that the member function (JetModel
) is different from the member property (model
). You set the jet's model as follows:
var sixthJet : Airplane; sixthJet = new Airplane(); sixthJet.JetModel = 757;
You get the jet's model similarly:
var seventhJet : Airplane; var myModel : int; seventhJet = new Airplane(); myModel = seventhJet.JetModel;
The class
object is part of ECMAScript Edition 4. We will cover classes more extensively in one of our next columns.
Next: A Final Word
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: April 22, 2002
Revised: April 22, 2002
URL: https://www.webreference.com/js/column108/6.html