{"componentChunkName":"component---src-templates-blog-post-js","path":"/JavaScript/javascript-concept-9---functional-programming-&-object-oriented-programming/","webpackCompilationHash":"f92dbf25810602c1889e","result":{"data":{"site":{"siteMetadata":{"title":"Web Devlog","author":"RossenaHuh","siteUrl":"https://github.com/rossenahuh","comment":{"disqusShortName":"RossenaHuh","utterances":"rossenahuh/my-dev-blog.git"},"sponsor":{"buyMeACoffeeId":"rossena"}}},"markdownRemark":{"id":"26492e7e-af1b-572e-8fd1-d433ada528ec","excerpt":"OOP (Object Oriented Programming)Combines a group of related variables and functions into an object.A programming paradigm based on the concept of objects, which can contain data, known as attributes, and code, often known as methods. With the methods of the same instance(objects…","html":"<h2 id=\"oop-object-oriented-programming\"><a href=\"#oop-object-oriented-programming\" aria-label=\"oop object oriented programming permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>OOP (Object Oriented Programming)</h2>\n<p><strong>Combines a group of related variables and functions into an object.</strong></p>\n<p>A programming paradigm based on the concept of <em>objects</em>, which can contain data, known as attributes, and code, often known as methods. With the methods of the same instance(objects have a notion of <em>this</em>, which refers to the object itself), we can easily modify the data fields of the object: <strong>altering the state</strong> of the object.</p>\n<p>Objects are instances of class. It works like a template which means we can esaily make a new instance of a certain attributes and methods.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">class</span> <span class=\"token class-name\">Dog</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">name<span class=\"token punctuation\">,</span> owner</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>name <span class=\"token operator\">=</span> name\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>owner <span class=\"token operator\">=</span> owner\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token function\">bark</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> <span class=\"token string\">'walf walf'</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">change_owner</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">owner</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>owner <span class=\"token operator\">=</span> owner\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">let</span> dog1 <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Dog</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Bori'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'Rossena'</span><span class=\"token punctuation\">)</span>\n  dog1<span class=\"token punctuation\">.</span>owner <span class=\"token comment\">// 'Rossena'</span>\n  dog1<span class=\"token punctuation\">.</span><span class=\"token function\">change_owner</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Dayoung'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  dog1<span class=\"token punctuation\">.</span>owner <span class=\"token comment\">// Now Bori's owner is 'Dayoung'</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Let’s say all dogs have a name and its owner. We can make a new instance of the dog class using new constructor with name and owner parameter. By using the change_owner method, we can also change the state of Bori.</p>\n<h2 id=\"benefits-of-oop\"><a href=\"#benefits-of-oop\" aria-label=\"benefits of oop permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Benefits of OOP</h2>\n<p><strong>1. Encapsulation</strong>\n<br>\nReduce complexity + increase reusability\n<br>\nUsing OOP, functions end up having fewer parameters, making them easier to use and maintain.</p>\n<p><strong>2. Abstraction</strong>\n<br>\nReduce complexity + isolate impact of changes\n<br>\nAll the complexity is hidden inside. Hiding some of the properties and methods form the outside. It provides simpler interface and reduces the impact of change because we don’t have any that touches these methods outside of the containing objects.</p>\n<p><strong>3. Inheritance</strong>\n<br>\nEliminate redundant code\n<br>\nFor example, when creating HTML elements intead of defining all the properties and methods, we can define them once in a generic object and utilize it.</p>\n<p><strong>4. Polymorphism (Many form)</strong>\n<br>\nRefactor ugly conditional statements\n<br>\nThe way of each HTML element is rendered is different from the others. With OOP, we can implement render method in each of these objects and the render method will behave differently depending on the type of the object we are referencing.</p>\n<h2 id=\"functional-programming\"><a href=\"#functional-programming\" aria-label=\"functional programming permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Functional Programming</h2>\n<p>A paradigm that <strong>focuses on the computation of pure functions</strong>.</p>\n<ul>\n<li>Complete separation between data and functions</li>\n<li>All objects created in functional programming are immutable</li>\n<li>Do not share scope with other objects</li>\n</ul>\n<h2 id=\"pure-functions\"><a href=\"#pure-functions\" aria-label=\"pure functions permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Pure functions</h2>\n<ul>\n<li><em>Always</em> return the same value with the same input value</li>\n<li>There are no side effects that might affect in the change of return value</li>\n<li>Do not alter the passed data</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">double</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">num</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> num <span class=\"token operator\">*</span> <span class=\"token number\">2</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// The outcome of double only depends on the input num</span></code></pre></div>\n<h2 id=\"benfits-of-functional-programming\"><a href=\"#benfits-of-functional-programming\" aria-label=\"benfits of functional programming permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Benfits of Functional Programming</h2>\n<p><strong>1. Predictable</strong></p>\n<ul>\n<li>gives certain guarantees on what would out come be</li>\n</ul>\n<br>\n<p><strong>2. Safe</strong></p>\n<ul>\n<li>Immutable state</li>\n<li>Needed change in the state, I can just create a new one</li>\n<li>Protect from typos and other error that could happen from mutable state</li>\n</ul>\n<br>\n<p><strong>3. Modular</strong></p>\n<ul>\n<li>like a building blocks with small units</li>\n<li>Empowers us to deduplicate the code</li>\n</ul>\n<h2 id=\"fp-vs-oop\"><a href=\"#fp-vs-oop\" aria-label=\"fp vs oop permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>FP vs OOP</h2>\n<p><strong>Fucntional Programming</strong></p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Create employees array</span>\n<span class=\"token keyword\">let</span> employees <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token punctuation\">:</span> <span class=\"token string\">'Dayoung'</span><span class=\"token punctuation\">,</span> salary<span class=\"token punctuation\">:</span> <span class=\"token number\">20000</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token punctuation\">:</span> <span class=\"token string\">'Sehyun'</span><span class=\"token punctuation\">,</span> salary<span class=\"token punctuation\">:</span> <span class=\"token number\">30000</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token punctuation\">:</span> <span class=\"token string\">'Yohan'</span><span class=\"token punctuation\">,</span> salary<span class=\"token punctuation\">:</span> <span class=\"token number\">25000</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">]</span>\n\n<span class=\"token comment\">// pure function</span>\n<span class=\"token comment\">// which returns a copy of a single employee with the salary field updated</span>\n<span class=\"token keyword\">let</span> <span class=\"token function-variable function\">change_salary</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">employee</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span> name<span class=\"token punctuation\">:</span> employee<span class=\"token punctuation\">.</span>name<span class=\"token punctuation\">,</span> salary<span class=\"token punctuation\">:</span> employee<span class=\"token punctuation\">.</span>salary <span class=\"token operator\">+</span> <span class=\"token number\">10000</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// Created a new datatset using the change_salary function</span>\n<span class=\"token comment\">// map returns a new altered dataset</span>\n<span class=\"token comment\">// while each alters the state of the object</span>\n<span class=\"token keyword\">let</span> happier_employees <span class=\"token operator\">=</span> employees<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">employee</span> <span class=\"token operator\">=></span> <span class=\"token function\">change_salary</span><span class=\"token punctuation\">(</span>employee<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\nhappier_employees\n<span class=\"token comment\">// [{name: 'Dayoung', salary: 30000},</span>\n<span class=\"token comment\">// {name: 'Sehyun', salary: 40000},</span>\n<span class=\"token comment\">// {name: 'Yohan', salary: 35000}]</span></code></pre></div>\n<p><strong>OOP</strong></p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Create Employee class</span>\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Employee</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">name<span class=\"token punctuation\">,</span> salary</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>name <span class=\"token operator\">=</span> name\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>salary <span class=\"token operator\">=</span> salary\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token function\">change_salary</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>salary <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>salary <span class=\"token operator\">+</span> <span class=\"token number\">10000</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// Create a instance of employee</span>\n<span class=\"token keyword\">let</span> employee1 <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Employee</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Dayoung'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">20000</span><span class=\"token punctuation\">)</span>\nemployee1<span class=\"token punctuation\">.</span>salary <span class=\"token comment\">// 20000</span>\n\n<span class=\"token comment\">// Use the each method to change salaray attribute of each employee</span>\nemployee1<span class=\"token punctuation\">.</span><span class=\"token function\">change_salary</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\nemployee1<span class=\"token punctuation\">.</span>salary <span class=\"token comment\">// 30000</span></code></pre></div>\n<br>\n<br>\n<p><strong>Sources</strong>\n<br>\n<a href=\"https://medium.com/@sho.miyata.1/the-object-oriented-programming-vs-functional-programming-debate-in-a-beginner-friendly-nutshell-24fb6f8625cc\">https://medium.com/@sho.miyata.1/the-object-oriented-programming-vs-functional-programming-debate-in-a-beginner-friendly-nutshell-24fb6f8625cc</a>\n<a href=\"https://www.youtube.com/watch?v=pTB0EiLXUC8\">https://www.youtube.com/watch?v=pTB0EiLXUC8</a>\n<a href=\"https://www.youtube.com/watch?v=FYXpOjwYzcs\">https://www.youtube.com/watch?v=FYXpOjwYzcs</a></p>\n<br>\n<br>","frontmatter":{"title":"JavaScript Concept 9 - Functional Programming & Object Oriented Programming","date":"July 14, 2019"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/JavaScript/javascript-concept-9---functional-programming-&-object-oriented-programming/","previous":{"fields":{"slug":"/JavaScript/javascript-concept-8---event-loop/"},"frontmatter":{"title":"JavaScript Concept 8 - Event Loop","category":"JavaScript"}},"next":{"fields":{"slug":"/JavaScript/javascript-concept-10---differences-between-es5-and-es6/"},"frontmatter":{"title":"JavaScript Concept 10 - ES5 vs ES6","category":"JavaScript"}}}}}