|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
ContentsIntroductionMany JavaScript programmers overlook or do not know of the ability to write object-oriented JavaScript. Whilst not conventionally an object-oriented language, JavaScript is a prototype-based language, which means that inherited classes are not derived directly from a base class, but rather are created by cloning the base class which serves as a prototype. This can be used to one's advantage in implementing encapsulation, inheritance and polymorphism in JavaScript, therefore creating a sense of object-orientation. Object-oriented JavaScript also has several advantages. As it is an interpreted language, it means that methods and properties can be added to the class at any time, and do not have to be declared in the class constructor, like other object-oriented languages such as C++. As JavaScript supports variable data types, class properties do not have to take a fixed data type (such as PrerequisitesAlthough this is an introductory article to object-oriented JavaScript, it would be beneficial to have an understanding of object-oriented programming, as this article does not go into this aspect in too much detail. However, a list of key object-oriented programming terms are listed and defined below for some guidance in this respect. Key TermsSeveral key terms will be used in this article which are summarized below:
A Simple Class in JavaScriptDefining a ClassA basic class can be implemented very easily in JavaScript. All that must be done in order to define a class is for a <script language="Javascript">
..
function MyClass()
{
}
..
</script>
These three lines of code create a new object called var c = new MyClass();
The function Creating Class PropertiesThis code so far is just a simple class with only its constructor declared. To add properties to the class, we use the ..
function MyClass()
{
this.MyData = "Some Text";
this.MoreData = "Some More Text";
}
..
These properties can be accessed by: var c = new MyClass();
alert(c.MyData);
This piece of code adds the property Creating Class MethodsAs said earlier in the article, class methods are created using the ..
function MyClass()
{
//Any properties created here
}
MyClass.prototype.MyFunction = function()
{
//Function code here
}
..
For clarity, the method here is created by using ..
function MyClass()
{
this.MyData = "Some Text";
}
MyClass.prototype.MyFunction = function(newtext)
{
this.MyData = newtext;
alert("New text:\n"+this.MyData);
}
..
This piece of code creates the EncapsulationEncapsulation is a useful part of object-oriented programming that "encapsulates" or contains data in an instance of a class from the data in another instance of the same class. This is why the Public, Protected and Private MembersEncapsulation is implemented in JavaScript by separating instance data within a class. However, there is no varying scale of encapsulation through the Encapsulation in PracticeAn example of encapsulation can be shown below: ..
function MyClass()
{
this.MyData = "Some Text";
}
MyClass.prototype.MyFunction = function(newtext)
{
this.MyData = newtext;
alert("New text:\n"+this.MyData);
}
..
var c = new MyClass();
c.MyFunction("Some More Text");
var c2 = new MyClass();
c2.MyFunction("Some Different Text");
If called, Conclusion to EncapsulationEncapsulation is an important part of object-oriented programming, so that data in different instances of a class are separate from one another; this is implemented in JavaScript by using the InheritanceInheriting PropertiesAs said earlier in the article, there is no direct inheritance in JavaScript, as it is a prototype language. Therefore, for a class to inherit from another class, the ..
//Parent class constructor
function Animal(name)
{
this.name = name;
}
//Inherited class constructor
function Dog(name)
{
Animal.call(this, name);
}
Dog.prototype = new Animal();
Dog.prototype.ChangeName(newname)
{
this.name = newname;
}
..
In the code example above, two classes are created — a base class called When an inherited class is constructed, two lines of code are needed to inherit from the base class, as demonstrated with Animal.call(this, name);
This line of code is called from within the subclass's constructor. The second line of code needed to inherit from a base class is: Dog.prototype = new Animal();
What this line of code does is set the prototype for the inherited class (which will clone the parent constructor when used) to be a new instance of the parent class, therefore inheriting any methods or properties in methods in the subclass. Notice also that once a subclass has inherited from a parent class, any data that needs to be accessed from the parent class can be accessed using the Inheriting MethodsLike properties, methods can also be inherited from a parent class in JavaScript, similar to the inheritance of properties, as shown below: ..
//Parent class constructor
function Animal(name)
{
this.name = name;
}
//Parent class method
Animal.prototype.alertName = function()
{
alert(this.name);
}
//Inherited class constructor
function Dog(name)
{
Animal.call(this, name);
this.collarText;
}
Dog.prototype = new Animal();
Dog.prototype.setCollarText = function(text)
{
this.collarText = text;
}
..
An inherited method can be called as so: var d = new Dog("Fido");
d.alertName();
This would call Creating an Instance of an Inherited ClassClasses that inherit from another class can be called in JavaScript as a base class would be called, and methods and properties can be called similarly. var d = new Dog("Fido"); //Creates an instance of the subclass
alert(d.name); //Retrieves data inherited from the parent class
d.setCollarText("FIDO"); //Calls a subclass method
Methods that are inherited in a subclass can also be called similarly with the variable name in place of the var d = new Dog("Fido"); //Creates an instance of the subclass
d.alertName(); //Calls the inherited method
Conclusion to InheritanceInheritance is the one of three important concepts of object-oriented programming. It is implemented in JavaScript using the PolymorphismPolymorphism is an extension on the principle of inheritance in object-oriented programming and can also be implemented in JavaScript using the ..
//Parent class constructor
function Animal(name)
{
this.name = name;
}
Animal.prototype.speak = function()
{
alert(this.name + " says:");
}
//Inherited class "Dog" constructor
function Dog(name)
{
Animal.call(this, name);
}
Dog.prototype.speak = function()
{
Animal.prototype.speak.call(this);
alert("woof");
}
//Inherited class "Cat" constructor
function Cat(name)
{
Animal.call(this, name);
}
Cat.prototype.speak = function()
{
Animal.prototype.speak.call(this);
alert("miaow");
}
..
This code means that if an instance of If called, this would look like: var d = new Dog("Fido"); //Creates instance of Dog
d.speak(); //Calls Dog's speak() function
var c = new Cat("Lucy"); //Creates instance of Cat
c.speak(); //Calls Cat's speak() function
The first two lines would alert "Fido says:" (the parent class's The second two lines would alert "Lucy says:" (the parent class's Conclusion to PolymorphismPolymorphism is a very useful part of object-oriented programming, and whilst in this article it is not pursued too deeply, the principles remain constant and can be applied like this in most aspects of polymorphism in JavaScript. ConclusionAfter reading this article you should be able to:
This is just an introductory article, but I hope you can use these learned skills for more complicated object-oriented structures. History
|
||||||||||||||||||||||