OOPS Concept in Javascript

Here is the sample code to show oops concept in javascript.


<script type="text/javascript" >
//Declare Class
function myClass() {
//Member Variable
this.memberVariable = '';
//Method
this.methodTest = function(obj) {
alert(this.memberVariable + ' ' + obj);
}
}
//Extend Class
myClass.prototype.newMethodTest = function(obj) {

alert(this.memberVariable + ' ' + obj);
}
//Static Method
myClass.myStaticMethod = function(obj) {
alert('This is static method with arg '+ obj);
}

//derived Class
derivedClass.prototype = new myClass();
function derivedClass() {
//New member variable
this.newMemberVariable = '';
//Override New method
this.newMethodTest = function() {
alert('old value' + this.memberVariable + 'new value' + this.newMemberVariable);
}
}




var obj = new derivedClass();
obj.newMemberVariable = 'New Test';
obj.memberVariable = 'Old Test';
obj.newMethodTest();
obj.methodTest('arg');




</script>


1 comment:

  1. that's all cool & great js tips for beginners, thank you very much for sharing.

    ReplyDelete