<script type="text/javascript" charset="utf-8">
// Root Namespace
var Namespace = {};
// Create a Class
Namespace.Class1 = function () { };
// Pulic Methods
Namespace.Class1.prototype.M1 = function () {
alert("M1");
};
// Create a Class with variables and members
Namespace.Class2 = function () {
// Public Variable
this.x = "Pubilic variable";
// Private Variable
var y = "private variable";
// Call private method. Only Constructor can call it
privateMethod();
// Private Method
function privateMethod() {
// Can access private variable
//Can not access public variable
y = 5;
}
//Privilaged method. Can access both public and private
// call publically
this.privatePublicMethod = function () {
// access public variable
this.x = 10;
// access private variable
y = 15;
// accessing private method
privateMethod();
};
};
// Pulic Method and access the Public variable
Namespace.Class2.prototype.M2 = function () {
alert(this.x);
};
Namespace.Class3 = function () { };
Namespace.Class3.prototype = new Namespace.Class1();
Namespace.Class3.prototype.M3 = function () {
alert("M3");
};
//Usage
var a = new Namespace.Class1();
a.M1();
var b = new Namespace.Class2();
alert(b.x);
b.M2();
b.privatePublicMethod();
var c = new Namespace.Class3();
c.M1(); // Inherited form Class 1
c.M3(); // Class 3 implementations
</script>
Comments