<?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: Justice Otuya</title>
    <description>The latest articles on DEV Community by Justice Otuya (@justiceotuya).</description>
    <link>https://dev.to/justiceotuya</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%2F54171%2F6f112657-7bfa-46b2-b346-5698e9080939.jpeg</url>
      <title>DEV Community: Justice Otuya</title>
      <link>https://dev.to/justiceotuya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justiceotuya"/>
    <language>en</language>
    <item>
      <title>Why should I use a getter and setter function instead of regular functions in a javascript Class?</title>
      <dc:creator>Justice Otuya</dc:creator>
      <pubDate>Mon, 29 Nov 2021 05:59:46 +0000</pubDate>
      <link>https://dev.to/justiceotuya/why-should-i-use-a-getter-and-setter-function-instead-of-regular-functions-in-a-javascript-class-fh7</link>
      <guid>https://dev.to/justiceotuya/why-should-i-use-a-getter-and-setter-function-instead-of-regular-functions-in-a-javascript-class-fh7</guid>
      <description>&lt;p&gt;I am reading about Object oriented programming and i am at a loss why i should use getters and setters instead of just regular functions.&lt;/p&gt;

&lt;p&gt;take this code for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack {
    constructor(){
        this.stack = []
    }

   ...
    get lengthWithSetter(){
        console.log('length')
    }

    lengthWithFunction(){
        console.log('length')
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;&lt;code&gt;const newStack = new Stack()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;to call the lengthWithSetter method (accessor)&lt;/p&gt;

&lt;p&gt;i can call it like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newStack.lengthWithSetter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while calling the lengthWithFunction method while calling the lengthWithSetter with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newStack.lengthWithFunction()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;my question is since newStack.lengthWithFunction() is consistent and has the structure of calling a normal function, while then do we have getters and setters.&lt;/p&gt;

&lt;p&gt;because reading a code with getters and setters make it seem like they are properties and not methods (don't slander me for this).&lt;/p&gt;

&lt;p&gt;So please can someone help answer this question, why do we need getters and setters when we can use regular functions&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
