Skip to content

Object Oriented Programming

IT Notes → Javascript @ December 22, 2020

  • Javscript is a class free object-oriented language that uses prototypal inheritance instead of classical inheritance.
  • function myClass() {                     //Function definition
    this.prop =prop1;                        //Function property
    this.method = method1;                   //Function method
    this.prototype.method = function () {}   //Usage of prototype
    }   
    
    function method1 () {
    }   
    
    var myClassObj = new myClass();         //Object declaration
    myClassObj.prop=2;                      //Object property initialized
    myClass.inherits(myBaseClass1)          //Inheritance
    myClass.swiss(myBaseClass2, fn)         //Swiss Inheritance
    MyClass.newmethod1=function() {}        //Class Augmentation
    myClassObj.newmethod2=function() {}     //Object Augmentation
  • We can see that a function can simulate an object. This keyword is used to specify that member is part of the function object. In the last line of the example, you can see the object property getting initialized.
  • We don’t need copies of member function. It needs to be created only once as in the case of object-oriented language. So, we attach keyword prototype. Prototype is extending to all instances. Like a template. This can be seen the line against the comment Usage of prototype.
  • We can keep on providing function definition outside class which can result in name clashes. Two classes cannot have same function name. So we provide definition inside class.
  • Also, we can utilize anonymous functions in which case we create function literals leading to closure.
  • function.method(“methodname” function(){}) is another way to create a member method.
  • To inherit we can use function1.inherits(function2).
  • To access a super method we use uber() like this.uber(“functionname”);
  • Swiss inheritance means inheriting certain requested methods. This disciplined approach is needed for multiple inheritance to prevent name collisions childfn.swiss(parentfn,method1, method2,…).
  • We can augment a class with methods and any present and future object will be augmented with the new method. This is known as class augmentation. Also, we can augment an object known as object augmentation. In this we add a method to an instance created with a new keyword.
  • In Parasitic inheritance we create an object in the constructor / function of the parent class and returns the object back after modifying its object methods. This leads to privileged methods.
  • function myClass(value) {
      var that = new baseClass(value);
      that.toString = function () {};            //Override the baseClass method
      return that;
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x