{"id":468,"date":"2013-06-13T14:43:07","date_gmt":"2013-06-13T14:43:07","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=468"},"modified":"2013-06-13T14:43:07","modified_gmt":"2013-06-13T14:43:07","slug":"javascript-encapsulation","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/javascript-encapsulation\/","title":{"rendered":"JavaScript &#8211; Encapsulation"},"content":{"rendered":"<p>In JavaScript everything is an object from the obvious, such as arrays, strings etc. through to functions. <\/p>\n<p>So how do we handle encapsulation such that we will have private and public data and methods ?<\/p>\n<p>Well, because functions are themselves objects we can declare private data internally (within the function) and then using the <strong>prototype<\/strong> property we can add methods to the function that are public.<\/p>\n<p>Below is a very simple example of encapsulation. The variable <em>balance<\/em> is private as it&#8217;s declared within the scope of the <em>BankAccount<\/em> function.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction BankAccount() {\r\n   var balance = 0;\r\n\r\n   BankAccount.prototype.add = function(amount) {\r\n      balance += amount;\r\n   }\r\n\r\n   BankAccount.prototype.subtract = function(amount) {\r\n      balance -= amount;\r\n   }\r\n\r\n   BankAccount.prototype.getBalance = function() {\r\n      return balance;\r\n   }\r\n};\r\n<\/pre>\n<p>The functions <em>add, subtract and getBalance<\/em> are public and thus we can interact with this object using the following<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar bankAccount = new BankAccount();\r\nbankAccount.add(100);\r\nbankAccount.subtract(10);\r\n\r\nvar current = bankAccount.getBalance();\r\n<\/pre>\n<p>If we wanted to make the balance variable public, we could simply alter the <em>var balance = 0<\/em> to <em>this.balance = 0<\/em> but of course this allows us to change the balance variable without going through the add\/subtract methods. <\/p>\n<p>To implement a more <em>property<\/em> like variable (as per C# for example) whereby we have methods to get\/set the property we might look to implement something like<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction BankAccount() {\r\n   var balance = 0;\r\n\r\n   this.__defineSetter__(&quot;balance&quot;, function(x) {});\r\n   this.__defineGetter__(&quot;balance&quot;, function() { return balance; });\r\n   \r\n   \/\/ rest of the methods as before\r\n};\r\n<\/pre>\n<p>Here we define a getter such that we can access in this way <em>bankAccount.balance<\/em> but we&#8217;ve defined a setter that does nothing so whilst a setter exists it doesn&#8217;t alter the balance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript everything is an object from the obvious, such as arrays, strings etc. through to functions. So how do we handle encapsulation such that we will have private and public data and methods ? Well, because functions are themselves objects we can declare private data internally (within the function) and then using the prototype [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[],"class_list":["post-468","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}