Javascript-OOPs(Part2)-AbstractionAndEncapsulation- (8)

  

👉Javascript with OOPs (Abstraction and Encapsulation)


Abstraction is showing what's necessary and Encapsulation is hiding the complexity.


For ex, if we are making calls to the server, we may have methods like:

this.CreateConnection = function() {}

this.CreateHttpObject = function() {}

Showing these methods to the user who is using this component may increase the complexity of the component.

User may have to call

         cust.CreateConnection();

         cust.CreateHttpObject();

         cust.Submit();


So  CreateConnection and CreateHttpObject has to be private, but the time you declare the methods using "this" keyword, it becomes public.

Below example solves this problem by creating private functions. If user makes a call to CreateConnection or CreateHttpObject, he will  receive "Not Found" error.


*Create private methods

function Customer(){ 

      function CreateConnection() {};

      function CreateHttpObject () {};

      this.submit = function() {

           CreateConnection();

           CreateHttpObject ();

           alert ("this submits the data using connection");

}


Abstraction is achieved in the design phase and we have achieved it through public "submit" method by encapsulating complex methods like CreateConnection and CreateHttpObject.


Summary : To implement abstraction and encapsulation in javascript, you have to make methods that are specific to the component and not relevant to the user as private and show only necessary methods using "this" keyword.

Comments

Popular posts from this blog

Javascript-OOPs(Part4)-Polymorphism- (10)

Javascript-OOPs(Part1)-Objects - (7)