Static methods in a class can be (and is) accessed without creating any object of that class. The static methods is called like Class.staticMethod() style.
As expected, JavaScript does not have any keyword like 'static'. But fortunately, this purpose can be served. Normal methods are defined inside class or using prototype. Static methods have to be defined outside the class and without prototype property.
Here a class TemperatureConverter has 2 static methods celsiusToFahrenheit (converts temperature in celcius to that in fahrenheit ) and fahrenheitToCelsius (opposite of previous function).
// define class
function TemperatureConverter()
{
}
//define static method
TemperatureConverter.celsiusToFahrenheit = function(celsius)
{
return (celsius * 9 / 5) + 32;
}
//define static method
TemperatureConverter.fahrenheitToCelsius = function(fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
Example:
var f = TemperatureConverter.celsiusToFahrenheit(35);
alert("35 degree celcius = " + f +" degree fahrenheit.");
var c = TemperatureConverter.fahrenheitToCelsius(98.4);
alert("98.4 degree fahrenheit = "+ c + " degree celcius.");
Static attributes can be added in similar fashion.
TemperatureConverter.description = "A class that converts temperature between celcius and fahrenheit";
08 November 2008
Static Method & Attribute in JavaScript Class
Posted by আলোর ছটা at 10:07 AM 0 comments
Labels: JavaScript, OOP
07 November 2008
OOP in JavaScript : Inheritance
There is no direct keyword (like 'extends' in Java) in JavaScript for inheritance. But its purpose can be served through 'prototype' attribute. Here, firstly an sample code is shown and then related points are described.
Sample Code of Inheritance:
function SuperClass(name)
{
this._name = name;
this.getName = function()
{
return this._name;
}
}
// inheritance occurs here
SubClass.prototype = new SuperClass;
function SubClass(name, address)
{
// calling constructor of super class
SuperClass.call(this,name);
//new attributes
this._address = address;
this.getAddress = function()
{
return this._address;
}
}
Object Creation & Usage:
Please observe the following code.
var sub = new SubClass("Alor Chhota","Mohammadpur");
alert("Name: " + sub.getName());
alert("Address: " + sub.getAddress());
Extending Super Class in Sub Class:
The following command asks to extend SuperClass in SubClass.
SubClass.prototype = new SuperClass;
Passing Parameters to SuperClass Constructor:
If you see the above code, you'll find the following line -
SuperClass.call(this,name);
This is actually the call of SuperClass constructor. Look closely at the sent parameters. The first parameter must be 'this' (SubClass) and the parameters of SuperClass will follow it. More descriptively, 2nd paramter of 'call' function is actually the 1st parameter of SuperClass, 3rd of call is 2nd of SuperClass etc.
Posted by আলোর ছটা at 6:03 PM 0 comments
Labels: JavaScript, OOP
OOP in JavaScript : Class
The OOP (Object Oriented Programming) concept is really poor in JavaScript. Fortunately, it supports class concept, although the keyword 'class' is not used. 'function' keyword serves the purpose of 'class'. There are several ways to implement class. Here I'm showing the simplest one (in my opinion).
Creating Class:
The following code creates a Person class with 4 attributes[ name, father, mother & address] and 2 methods [getName() & setAddress(address)].
function Person(name, father, mother, address)
{
this._name = name;
this._father = father;
this._mother = mother;
this._address = address;
this.getName = function()
{
return this._name;
}
this.setAddress = function(address)
{
this._address = address;
}
}
Note:
- The attributes are created and accessed through 'this' keyword.
- Person function is the constructor of the class which takes 4 parameters.
- The constructor (or function) can't be overloaded like C++ or Java.
Creating Instance (Object):
It's simple enough to understand from the following code.
var p1 = new Person("Ahona Saha", "Anup Kumar", "Snigdha Saha", "Bogra");
var p2 = new Person("Ananyo Mounota", "Asim Kumar Saha", "Aparna Saha", "Narsingdi");
Accessing Attributes and Calling Methods:
The attributes can be accessed like below-
alert(p1._name);
Method calling is similar.
alert(p2.getName());
You can also change the attribute value.
p1._address = "Sariakandi, Bogra";
Note:
- There is no Encapsulation in JavaScript i.e. any attributes is accessible from anywhere.
- Interesting (and sometime disgusting) thing is, you can add attributes to an object anytime from anywhere (not necessarily from class). For example- if you want to add another attribute '_gender' with object p1, you can do it like below
p1._gender = "female";
Posted by আলোর ছটা at 4:53 PM 0 comments
Labels: JavaScript, OOP