<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhavesh Daswani</title>
    <description>The latest articles on DEV Community by Bhavesh Daswani (@bhaveshdaswani93).</description>
    <link>https://dev.to/bhaveshdaswani93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F251698%2F7ca45af0-1103-408d-9aae-39905d5b1dc6.jpeg</url>
      <title>DEV Community: Bhavesh Daswani</title>
      <link>https://dev.to/bhaveshdaswani93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhaveshdaswani93"/>
    <language>en</language>
    <item>
      <title>Composition vs Inheritance</title>
      <dc:creator>Bhavesh Daswani</dc:creator>
      <pubDate>Sun, 05 Jan 2020 04:39:20 +0000</pubDate>
      <link>https://dev.to/bhaveshdaswani93/composition-vs-inheritance-318l</link>
      <guid>https://dev.to/bhaveshdaswani93/composition-vs-inheritance-318l</guid>
      <description>&lt;p&gt;Composition vs Inheritance&lt;br&gt;
Inheritance is about what it is while composition is all about what it has or what it's ability is.In Inheritance we pre-define the architecture like parent class is inherited by sub classes and this sub class is inherited by another sub classes and this nesting continues the more nesting we have the more rigid is our code that is more harder to change and some times our code may get fragile that is if we change in parent class it may effect sub class in unexpected manner.For example I have a Base class &lt;code&gt;Human&lt;/code&gt; then three sub class &lt;code&gt;young,old,adult&lt;/code&gt; then we have inner sub class for each of them which could be &lt;code&gt;male,female&lt;/code&gt;.But how this inner classes or inheritance make our code rigid or harder to change in future let's understand this issue with an example.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In class base inheritance i have created three classes Human -&amp;gt; Young -&amp;gt; Male&lt;br&gt;
The method of Young and Human class are available to Male class instance, this is what i mean the inheritance is all about what it is, we are predefine the relationship and there inheritance. Suppose in future we have certain requirement  for which we need to change the inheritance flow to Human-&amp;gt;Male-&amp;gt;Young. Some time it is possible to do change like this, but i am sure that in near future it will break because tight coupling that is Young class depend on Human, Male class depend on Young class will definitely will create issue in future because as a human we cannot predict future, changes will be there so how to resolve this. My preference is composition. Composition focuses on what is the ability, for that to understand i have created two function. A man function return an a instance with sleep ability which is provided by getSleepAbility function. So dividing the requirement into ability  and composing  required abilities is the answer to tight coupling which is caused by &lt;code&gt;what it is&lt;/code&gt; instead of &lt;code&gt;what ability it has&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>functional</category>
    </item>
    <item>
      <title>OOP vs FP with Javascript</title>
      <dc:creator>Bhavesh Daswani</dc:creator>
      <pubDate>Sat, 04 Jan 2020 07:46:06 +0000</pubDate>
      <link>https://dev.to/bhaveshdaswani93/oop-vs-fp-with-javascript-39jf</link>
      <guid>https://dev.to/bhaveshdaswani93/oop-vs-fp-with-javascript-39jf</guid>
      <description>&lt;p&gt;In this article i want to explain you about when to use what. For which problem functional programming is good and for which problem OOP is good. It is not necessary to use only one paradigm at a time we can combine them and use the power of both paradigm. One example that come into my mind is of react app which uses the power of both the paradigm,for statefull component we use class component(OOP) and for stateless component that is which mainly deal with view part was in functional component (before hooks were introduced).&lt;/p&gt;

&lt;p&gt;Before reading below i am assuming you are familiar with OOP and FP in javascript. If not please go through from my article i have written on OOP and FP&lt;br&gt;
&lt;a href="https://dev.to/bhaveshdaswani93/object-oriented-programing-in-javascript-3bp0"&gt;OOP in Javascript&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/bhaveshdaswani93/functional-programming-in-javascript-59e2"&gt;FP in Javascript&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;OOP vs FP:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;OOP programming organizes the code as a unit. Here the unit or the object contains the information and operation that belong to same concept. The pieces of the information that reside in the unit are called attribute or state. The operation that can be happen on state are called method. The pillars of OOP are - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encapsulation
&lt;/li&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FP consider data and operation as two different thing. It is all about avoiding side effect and writing pure functions.Functions should not modify its outer world and its return value depends on the argument provided. It is based on different concepts like Pure function, HOF(Hige Order Function), Refrential transparency etc. In it function are first class citizen. I think in FP  pure function and compose can provide you extremely powerful results&lt;/p&gt;

&lt;p&gt;Both OOP and FP are paradigm that is a design pattern for solving the common problem that is to make our code manageable , managable in the following sense:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8wrjv8rrp2u6akxgifrv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F8wrjv8rrp2u6akxgifrv.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clear and Under-stable: OOP and FP organizes the code in such a way that it becomes easier for other developer to understand over code and also it make our code mode cleaner&lt;/li&gt;
&lt;li&gt;Easy to extend: As the app grows and numbers of developers increases, this paradigms makes easier to extend the code.&lt;/li&gt;
&lt;li&gt;Easy to maintain: OOP and FP makes code cleaner, this also makes code more maintainable like fixing bug, adding functionality becomes more easier&lt;/li&gt;
&lt;li&gt;Memory efficient: In OOP we have concept of inheritance and in FP we have concept of closures which make program more memory efficient.&lt;/li&gt;
&lt;li&gt;DRY: Dry means Do not repeat yourself.This paradigm if truly followed it avoids duplication of code by using inheritance in OOP and reusable functions in FP&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Difference Between FP and OOP&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FP is good for many operation on fixed data while oop programming is good for few operation for common data&lt;/li&gt;
&lt;li&gt;FP is stateless means it does not modify the state of the program by return new state every time in immutable fashion while OOP is statefull, its method changes the state of its properties.&lt;/li&gt;
&lt;li&gt;FP is about having pure function that has no side effects while OOP has side effect as it modify its state.&lt;/li&gt;
&lt;li&gt;FP is declarative it focuses on what need to be done while OOP is imperative it focuses on how the things should be done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;When to use What:&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you have few things that requires lot of operation lot of little functions applied to it then FP is good options. Functional programming works really well for high performance and processors as you can run it on multiple processors simultaneously.&lt;/li&gt;
&lt;li&gt;If you have too many things like characters in game and few operation then OOP is good choice&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>oop</category>
    </item>
    <item>
      <title>Functional Programming in javascript</title>
      <dc:creator>Bhavesh Daswani</dc:creator>
      <pubDate>Sun, 29 Dec 2019 14:35:28 +0000</pubDate>
      <link>https://dev.to/bhaveshdaswani93/functional-programming-in-javascript-59e2</link>
      <guid>https://dev.to/bhaveshdaswani93/functional-programming-in-javascript-59e2</guid>
      <description>&lt;p&gt;My aim is that after you read this article, you understand what is a functional programming. There is lot of articles for OOP, myself have return an article on OOP, but when i started learning functional programming i started loving it. Its not about OOP vs FP(functional programing) but how to take advantage of both.&lt;/p&gt;

&lt;p&gt;In this article, I want to explain about functional programming and how good javascript support functional programming.&lt;/p&gt;

&lt;p&gt;What is Functional Programming?&lt;br&gt;
In any program there is two core thing data and behavior. Data could be array, object,hashmap etc. Data can be in any form. Behavior is function that perform operation on data. Functional programming says that data and behavior(function) are two different thing. they should be kept separate. It simply says that you pass data to function it will process it and return new object.&lt;/p&gt;

&lt;p&gt;There are many new terms in functional programming learning for first time will be exhaustive but my personal suggestion is that you should give this a try.&lt;/p&gt;

&lt;p&gt;Before understanding how Function programming works in javascript and its terms, It is necessary to understand that  function are first class citizen in js, and also closure and High order function. If you are unclear about them you can checkout on my blog on this, &lt;a href="https://rebrand.ly/javascript-closures-9d89f" rel="noopener noreferrer"&gt;https://rebrand.ly/javascript-closures-9d89f&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Functional programming is all about separation of concerns.It's all about packaging our code into separate chunks so that everything's is well organized in each part of our code.Functional programming says that data and behavior(function) are two different thing. they should be kept separate. The core pillar of functional programming is pure function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvmmdtxldnj3zaptvjpqw.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fvmmdtxldnj3zaptvjpqw.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;What are pure function?&lt;/b&gt;&lt;br&gt;
A function that follow below point are pure function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given the same input it will provide the same output no matters how many times we call it&lt;/li&gt;
&lt;li&gt;It does not modify its outer world that is, it has no side effect.
Let's understand with example&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above I have created two function removeLastItem and immutablyRemoveLastItem.&lt;br&gt;
The removeLastItem has side effect as it modify outer world variable &lt;code&gt;arr&lt;/code&gt; while immutablyRemoveLastItem function has no side effect because it first copy the external variable using concat method and then alter the new array(which it has ownership of) and return it. &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Let understand with example the concept of  same input then same out no matter how many time the function is called 
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

In above example I have created two function addTwoNumber,multiplyWithCurrentTime.
addTwoNumber is pure function because it does not have any side effect and also with same input it will provide same output. multiplyWithCurrentTime is not pure function because with same input it can generate different output depending on current time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;&lt;b&gt;There are some terms in functional programming let's define them&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Referential Transparency&lt;/b&gt;&lt;br&gt;
In functional programming, referential transparency is generally defined as the fact that an expression, in a program, may be replaced by its value (or anything having the same value) without changing the result of the program. This implies that methods should always return the same value for a given argument, without having any other effect.&lt;br&gt;
Let's understand it with example&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above example function &lt;code&gt;a&lt;/code&gt; Is referential transparent as it can be replaced by its value without effecting the result of the program while function &lt;code&gt;c&lt;/code&gt; is not referential transparent because here replacing with the value will effect the result of the program as function c has console.log which is one type of side effect.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Idempotence&lt;/b&gt;&lt;br&gt;
A function is idempotence if for same input it provides same output or does what we expect, Idempotence is different from pure function as it allow side effect. A n example could be get api  that with same input provide same output no matter how many times it is called.Another Feature of Idempotence is the idea of calling itself again and again and still the output is the same.Let see other example also:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above example there are three function notIdempotenceFn,idempotentFn and getAbsolute. In notIdempotenceFn function will result different output in each call so it is not idempotent while the function idempotentFn is idempotent as for same input it &lt;br&gt;
 will have same output that is console.log which will print the output to the console. One note idempotentFn function is not pure as it print in console that is altering the outside world. getAbsolute function is an idempotent function as it provide the same result no matter how many times i call it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Imperative vs Declarative&lt;/b&gt;&lt;br&gt;
Imperative code means what to do and how to do while declarative code means what to do and what need to be done it will not tell how to do it. Let's understand with an example&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above example we have one task to &lt;code&gt;console.log&lt;/code&gt; 1 to 5 and how this task can be done imperatively and declaratively. The for loop is imperative because here we define what to do that is &lt;code&gt;console.log&lt;/code&gt; and also how to do by defining variable &lt;code&gt;let i=1&lt;/code&gt;, its condition &lt;code&gt;i&amp;lt;=5&lt;/code&gt; and increment by 1 &lt;code&gt;i++&lt;/code&gt;. The other example is &lt;code&gt;forEach&lt;/code&gt; loop which is declarative because here we specify what to do that is &lt;code&gt;console.log&lt;/code&gt;  and not how to do which is manage by forEach function.&lt;br&gt;
Why i am teaching you about Imperative vs Declarative because functional programming help us to be more declarative by using compose which we will learn later. compose tell our programs what to do instead of how to do it.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Immutability&lt;/b&gt;&lt;br&gt;
Immutability means not to modify original state by copying it and then applying required changes to  the new state and returning the new state. Let's see an example &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In above example we have two function mutatingState and immutatingState. The function mutatingState changes the orignal state while immutatingState function creates a copy of the orignal state and return new state. Functional programming recommends immutability as immutability provide stability and predictability to our code. We will come to know the importance of immutability when we understand composing.

&lt;p&gt;&lt;b&gt;High Order Function&lt;/b&gt;&lt;br&gt;
In javascript function are first class citizen. First class citizen means function can be pass as argument,function can be a return value, function can be assign to variable.&lt;br&gt;
What is High Order Function?&lt;br&gt;
A function that receive function as an argument or a function whose return value is function,such function is a High order Function. let's see with an example&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In above example we have two function hocFn and hocFn2. hocFn function return function so it is HOC while hocFn2 accept function as argument so it is also HOC.

&lt;p&gt;&lt;b&gt;Closure&lt;/b&gt;&lt;br&gt;
Like object,Closures in JavaScript are a mechanism for containing some sort of state and in JavaScript we create a closure whenever a function accesses a variable defined outside of the immediate function scope that is the scope of the parent. And it's fairly easy to create a closure.We simply define a function inside another function and expose the inner function either by returning&lt;br&gt;
it or passing it to another function so that we can use that variable.&lt;/p&gt;

&lt;p&gt;I have written separate blog on closure be sure to check on that&lt;br&gt;
&lt;a href="https://rebrand.ly/javascript-closures-9d89f" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://rebrand.ly/javascript-closures-9d89f" rel="noopener noreferrer"&gt;https://rebrand.ly/javascript-closures-9d89f&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Currying&lt;/b&gt;&lt;br&gt;
Currying is a technique of translating a function evaluation that takes multiple parameter into evaluating multiple function that each takes a single parameter.&lt;br&gt;
Let's understand with an example&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In above example i have created two function multiply and currying. The multiple function takes two parameter while the currying function take single parameter at time. In this example i have tried to show how we can convert a function with multiple parameter &lt;code&gt;multiply(a,b)&lt;/code&gt; into multiple function with single parameter &lt;code&gt;curring&lt;/code&gt;.

&lt;p&gt;&lt;b&gt;Partial Application&lt;/b&gt;&lt;br&gt;
Partial Application means we are partially applying a function. Suppose a function has 5 arguments. We want its execution to be partially that is for now i will pass 2 arguments and rest of the 3 arguments i will pass later, this is called partial application and it is possible due to closure because when we apply function partially the argument we passed are remembered and are used when we fully execute the function with remaining number of arguments. Let's understand with example.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above example  partiallyMultiplyBy5 partially apply multiply function with 5 as first argument. When executing the partiallyMultiplyBy5 function we just have to pass remaining parameter as the first argument 5 has been remember due to closure.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Memoization:&lt;/b&gt;&lt;br&gt;
Memoization is a special form of caching. Memoization cache the return value of the function based on its parameter that is if the parameter does not change then the return value is memoized. let's see with an example&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above example we have two function notMemoized and memoizedFn. notMemoized function will execute the function logic of multiplication for each execution also if the parameter is same. While for memoizedFn the function logic of multiplication will only be executed if the result is not cached ,for second time with same parameter the value will return from cache.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Compose and Pipe&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Composing is an idea that describe that the transformation of the data should be obvious. Let's describe compose in simple terms: if there is a data which is processed by a  function and that function return new form of the data, the return data is again processed by another function which return new form of data and this chain continues until we get required output. We can say for compose that it is a design principle which describe the relationship with different components (function), here we arrange components in a assembly line which describe how the data is transformed from one function to another.&lt;br&gt;
Pipe is similar to compose the difference is in execution. compose execute the components from right to left while pipe execute the component from left to write.&lt;/p&gt;

&lt;p&gt;let see with an example&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above example i have tried to explain how we can use compose to transform the data, In the example there is requirement to multiple a number with 3 and then get absolute of the number. These are two different operation so i have created two function multiplyWith3,getAbsouleOfNum which are pure function. Now if we do not use compose then first we have to call multiplyWith3 function store it output in the variable , then use that variable to call getAbsouleOfNum function to get the desired result, this is a one way to do it. Let's now do in compose way, here  we have two component(function) multiplyWith3,getAbsouleOfNum we can arrange them in a sequence in a manner that output of one program is input of another so i have created multiplyBy3andGetAbsolute function which will first execute getAbsouleOfNum and then the output of the getAbsouleOfNum function will be provided to multiplyWith3. We can also do this in the pipe way for that i have created multiplyBy3andGetAbsolutePipe here first muliplyBy3 is executed whose output is passed to getAbsouleOfNum &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Arity&lt;/b&gt;&lt;br&gt;
Arity mean number of argument the function takes. It is preferred to have less number of argument to a function to make it more usable. My preference for number  of argument a function should have is 1 or 2. Let's see with an example&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In above example I have created two function addNumber and getAbsoulte. addNumber  function has arity of 2 as it has two argument while getAbsoulte has arity of 1 as it has one argument.&lt;/p&gt;




&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fq52fv92lwhbawmb4udtx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fq52fv92lwhbawmb4udtx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Functional programming suggests that the data and function(effect) should be separate. The function should have following properties&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single task: A Function should be small and should perform single task&lt;/li&gt;
&lt;li&gt;Pure: Function should not have side effect and for same input it should provide same out&lt;/li&gt;
&lt;li&gt;Should have return statement.&lt;/li&gt;
&lt;li&gt;should be compose-able&lt;/li&gt;
&lt;li&gt;Immutable: Function should return a new copy of data and should not change the original state&lt;/li&gt;
&lt;li&gt;Should be predictable&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>Object Oriented Programing in Javascript</title>
      <dc:creator>Bhavesh Daswani</dc:creator>
      <pubDate>Sun, 22 Dec 2019 07:11:31 +0000</pubDate>
      <link>https://dev.to/bhaveshdaswani93/object-oriented-programing-in-javascript-3bp0</link>
      <guid>https://dev.to/bhaveshdaswani93/object-oriented-programing-in-javascript-3bp0</guid>
      <description>&lt;p&gt;What is OOP(Object Oriented Programing)?&lt;br&gt;
OOP is a programming pattern that says the state(property) and the action(method) should be kept together in a single box(class).OOP tries to model real-world objects. It is useful to organize our code.&lt;/p&gt;

&lt;p&gt;There are two types of OOP languages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class based languages like java, C#, php,C++ etc&lt;/li&gt;
&lt;li&gt;Prototype based languages javascript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are four principal/pillars of OOP:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F07oqqtlnul7sgsuxrn08.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F07oqqtlnul7sgsuxrn08.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Encapsulation:

&lt;ul&gt;
&lt;li&gt;OOP puts things(code) into an object and organizes the things in units that model the real world this is encapsulation.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Abstraction

&lt;ul&gt;
&lt;li&gt;OOP hides the complexity and the implementation detail, we just have to call the method and the rest of the complexity are hidden from us, this is an abstraction. Abstraction also provide a layer of security like private property or private method&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Inheritance

&lt;ul&gt;
&lt;li&gt;Inheritance means reusing of the code, that is extending from parent class where all the property and method (protected or public) are available to the child class&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Polymorphism

&lt;ul&gt;
&lt;li&gt;Polymorphism means many forms(Poly means many; morphism means form). But how it's related to OOP. It is related to OOP by concept called overriding and overloading. &lt;/li&gt;
&lt;li&gt;overriding: Suppose we have method name getMarks in parent class and with the same name we have a method in child class which will override the functionality of parent class, this is overriding. It is useful when we want custom logic for child class still using the some of the logic from the parent class&lt;/li&gt;
&lt;li&gt;overloading: In same class we can have two or more method with same name but with different number of argument or with different data type, this is called overloading.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Above was the definition and principal of OOP. Now lets understand how to implement OOP in javascript.&lt;/p&gt;

&lt;p&gt;For understanding OOP in javascript, you should have a clear knowledge of prototypal inheritance in javascript, If you are unclear on that then please check my blog on it &lt;a href="https://dev.to/bhaveshdaswani93/prototype-inheritance-in-javascript-1c3g"&gt;https://dev.to/bhaveshdaswani93/prototype-inheritance-in-javascript-1c3g&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have four ways for working with OOP in javascript:&lt;br&gt;
1.Factory function.&lt;br&gt;
2.Function constructor&lt;br&gt;
3.Object.create()&lt;br&gt;
4.ES6 classes&lt;/p&gt;

&lt;p&gt;Let see them one by one&lt;/p&gt;

&lt;p&gt;&lt;b&gt;1.Factory Function:&lt;/b&gt;&lt;br&gt;
In this, we create a function that receives parameter and in return, it provides an object, let see how it is useful in avoiding duplication of code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Here in the example, personA and personB both have getFullName function definition which indicate duplication of the code which is not good programming practice. To avoid that we have created a factory function called person in which we pass the first_name and last_name as a parameter and it will provide the required object. By using factory function we have avoided repeating our code. But here is a problem with memory efficiency as getFullName function will be created each time the person function is called which is not good because if we can place getFullName in commonplace in memory where each new person object can call it then it would also be memory efficient, for to achieve this let's move to constructor function. 

&lt;p&gt;&lt;b&gt;2.Function constructor:&lt;/b&gt; &lt;br&gt;
What is a constructor function? A function that is initiated with a &lt;code&gt;new&lt;/code&gt; keyword is a constructor function and the constructor function should start with a capital letter ( It is good practice to name constructor function with the first letter as capital like Person is good against person ). Let see this in an example and how it helps to be memory efficient.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above example Person is the constructor function and it has getFullName function in its prototype object, I have created two instance personA, personB from Person constructor. We can see that the code is resuable by having a constructor and prototype. Constructor function has that code which is unique to an instance that means personA and personB has there own first_name and last_name properties while the prototype has that code which is shared by the instance and also prototype properties are not copied to the instance, they are resolved through prototype chain which makes constructor function memory efficient.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;3.Object.create():&lt;/b&gt; &lt;br&gt;
The &lt;code&gt;Object.create()&lt;/code&gt; method creates a new object, using an existing object as the prototype of the newly created object. In earlier section we have learned how to create prototype using constructor function, let see how we can create prototype using &lt;code&gt;Object.create()&lt;/code&gt; with an example&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above example, I have created a person object and use it as the prototype of the personA object using &lt;code&gt;Object.create(person)&lt;/code&gt;. Object.create will create a prototype chain where &lt;code&gt;__proto__&lt;/code&gt; property of personA will point to person object.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;4.ES6 classes:&lt;/b&gt; &lt;br&gt;
ES6 brings the keyword class which is familiar to most of the OOPS developers. Class in javascript is like a syntactical sugar behind the scenes it still follows prototypal inheritance. The class keyword was brought to bring simplicity and easiness for the developers to write OOP programming in javascript. let's see classes in action.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Above is an example of class. I have created Person class which contains the constructor function whose properties and method will be copied to each instance while the rest of the method or properties like getFullName is shared.&lt;br&gt;
Let me provide you a cheat sheet for classes: anything you want to be unique to the instance like first_name, last_name is unique to each instance, place that property or method in the constructor function and shareable property or method like getFullName should be placed outside the constructor and should be inside the class.&lt;/p&gt;




&lt;br&gt;&lt;br&gt;
&lt;b&gt;Conclusion:&lt;/b&gt;&lt;br&gt;&lt;br&gt;
OOP is a programming style that helps you to write clear, understandable, easy to extend, easy to maintain, memory efficient and DRY code.&lt;br&gt;&lt;br&gt;
In Javascript, we have 4 ways to write OOP code

&lt;ul&gt;
&lt;li&gt;Factory function&lt;/li&gt;
&lt;li&gt;Constructor function&lt;/li&gt;
&lt;li&gt;Object.create&lt;/li&gt;
&lt;li&gt;Classes&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
    </item>
    <item>
      <title>Prototype Inheritance in Javascript</title>
      <dc:creator>Bhavesh Daswani</dc:creator>
      <pubDate>Sun, 08 Dec 2019 06:52:46 +0000</pubDate>
      <link>https://dev.to/bhaveshdaswani93/prototype-inheritance-in-javascript-1c3g</link>
      <guid>https://dev.to/bhaveshdaswani93/prototype-inheritance-in-javascript-1c3g</guid>
      <description>&lt;p&gt;There are two main pillars of javascript &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Closure&lt;/li&gt;
&lt;li&gt;Prototypal Inheritance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Closure, you can check my blog on &lt;a href="https://rebrand.ly/javascript-closures-9d89f"&gt;https://rebrand.ly/javascript-closures-9d89f&lt;/a&gt;&lt;br&gt;
Let's understand Prototype inheritance with a basic example as simple Example makes understanding topics easier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YMCahZsn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ouhyf952zc19kkuvqmk1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YMCahZsn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ouhyf952zc19kkuvqmk1.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I created variable two variable &lt;code&gt;a,b&lt;/code&gt; which is an array and then try to concat them using &lt;code&gt;a.concat(b)&lt;/code&gt;.You will be thinking where is prototypal inheritance here in the example. It is there, &lt;code&gt;a&lt;/code&gt; variable does not have concat method and then also &lt;code&gt;a.contcat(b)&lt;/code&gt; is working but how, because of prototypal inheritance. Variable  &lt;code&gt;a&lt;/code&gt; has a special property &lt;code&gt;__proto__&lt;/code&gt; which is pointing to Array and the Array has the concat method, so it's working.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w2r7koCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x5gd0o5dj3jogpva2utx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w2r7koCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/x5gd0o5dj3jogpva2utx.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am again using a simple example as it explains the core concept very easily. Here I want you to understand the prototypal chain. In this example, I have created the variable &lt;code&gt;a&lt;/code&gt; that is an array. I am using a method &lt;code&gt;a.toString()&lt;/code&gt; which is not a property or method of the variable &lt;code&gt;a&lt;/code&gt; so how does it resolve let's understand. If the javascript engine can not find a property or method in the main variable then it will look the &lt;code&gt;__proto__&lt;/code&gt; property. &lt;code&gt;__proto__&lt;/code&gt; property will points to its parent here which is Array, now the javascript engine will look in the Array and try to find the &lt;code&gt;toString&lt;/code&gt; method which is not available in Array, Array also has &lt;code&gt;__proto__&lt;/code&gt; property which points to base Object(I will explain what is base Object). Now the javascript engine will look in the base Object for the &lt;code&gt;toString&lt;/code&gt; method which is available there and from the base Object, it gets resolved.&lt;/p&gt;

&lt;p&gt;Note: Base Object is the very core point in javascript, you can say it is the very end for prototype chain lookup as the &lt;strong&gt;proto&lt;/strong&gt; property of the base Object points to null. If the javascript engine cannot resolve the property or method until up to the base Object then it will throw an error.&lt;/p&gt;

&lt;p&gt;Let's understand two most confusing terms &lt;code&gt;__proto__&lt;/code&gt; property and &lt;code&gt;prototype&lt;/code&gt; object in prototypal inheritance, one of them &lt;code&gt;__proto__&lt;/code&gt; would have been clear from above examples.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9XrDcviq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zrst9nbd1zgjbyg5m6gi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9XrDcviq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/zrst9nbd1zgjbyg5m6gi.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;code&gt;__proto__&lt;/code&gt; points to the parent &lt;code&gt;prototype&lt;/code&gt; object. In the second example of &lt;code&gt;a.toString()&lt;/code&gt;  &lt;code&gt;a.__proto__&lt;/code&gt; property points to the &lt;code&gt;Array.prototype&lt;/code&gt;, let's conclude this with an example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OAhGoXiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bcqoa8w1hj1mf9zjz0su.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OAhGoXiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bcqoa8w1hj1mf9zjz0su.PNG" alt="Alt Text"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;From the above example, I want you to convey that the &lt;code&gt;__proto__&lt;/code&gt; property of variable points to the parent &lt;code&gt;prototype&lt;/code&gt; object. In this example I have created variable &lt;code&gt;a&lt;/code&gt; which is an array and the &lt;code&gt;a.__proto__&lt;/code&gt; points to &lt;code&gt;Array.prototype&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: If you want to check that a property belongs to the variable itself not the prototype chain you have a method hasOwnProperty which works as follows &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I9PAICen--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8xqb251dn4e6hcfrht2o.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I9PAICen--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/8xqb251dn4e6hcfrht2o.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Variable &lt;code&gt;b&lt;/code&gt; is an object with property &lt;code&gt;name&lt;/code&gt; so &lt;code&gt;b.hasOwnProperty('name')&lt;/code&gt; return true while &lt;code&gt;b.hasOwnProperty('toString')&lt;/code&gt; return false as &lt;code&gt;toString&lt;/code&gt; method belongs to the parent.&lt;/p&gt;

&lt;p&gt;Let me conclude this blog with an idea of how memory efficient is a prototypal inheritance. Let reference again the example of &lt;code&gt;a.concat&lt;/code&gt; suppose if all the method of an array is copied to each new variable then how much memory each variable will consume. so by prototypal inheritance, every method of array resides in one place of memory and is referenced by each new variable, this type of referencing makes prototypal inheritance very memory efficient. &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
