{"id":908,"date":"2017-02-03T14:42:08","date_gmt":"2017-02-03T13:42:08","guid":{"rendered":"http:\/\/ndk.sytes.net\/wordpress\/?p=908"},"modified":"2017-02-03T14:42:08","modified_gmt":"2017-02-03T13:42:08","slug":"oop-in-javascript","status":"publish","type":"post","link":"https:\/\/ndk.sytes.net\/wordpress\/?p=908","title":{"rendered":"OOP in javascript"},"content":{"rendered":"<p>\n\twell explained public, private and protected variables and methods in javascript:\n<\/p>\n<p>\n\t<a href=\"http:\/\/phrogz.net\/js\/classes\/OOPinJS.html\" target=\"_blank\">http:\/\/phrogz.net\/js\/classes\/OOPinJS.html<\/a>\n<\/p>\n<pre>\n<code>function Person(n,race){&nbsp;\n\tthis.constructor.population++;\n\n\t\/\/ ************************************************************************&nbsp;\n\t\/\/ PRIVATE VARIABLES AND FUNCTIONS&nbsp;\n\t\/\/ ONLY PRIVELEGED METHODS MAY VIEW\/EDIT\/INVOKE&nbsp;\n\t\/\/ ***********************************************************************&nbsp;\n\tvar alive=true, age=1;\n\tvar maxAge=70+Math.round(Math.random()*15)+Math.round(Math.random()*15);\n\tfunction makeOlder(){ return alive = (++age &lt;= maxAge) }&nbsp;\n\n\tvar myName=n?n:&quot;John Doe&quot;;\n\tvar weight=1;\n\n\n\t\/\/ ************************************************************************&nbsp;\n\t\/\/ PRIVILEGED METHODS&nbsp;\n\t\/\/ MAY BE INVOKED PUBLICLY AND MAY ACCESS PRIVATE ITEMS&nbsp;\n\t\/\/ MAY NOT BE CHANGED; MAY BE REPLACED WITH PUBLIC FLAVORS&nbsp;\n\t\/\/ ************************************************************************&nbsp;\n\tthis.toString=this.getName=function(){ return myName }&nbsp;\n\n\tthis.eat=function(){&nbsp;\n\t\tif (makeOlder()){&nbsp;\n\t\t\tthis.dirtFactor++;\n\t\t\treturn weight*=3;\n\t\t} else alert(myName+&quot; can&#39;t eat, he&#39;s dead!&quot;);\n\t}&nbsp;\n\tthis.exercise=function(){&nbsp;\n\t\tif (makeOlder()){&nbsp;\n\t\t\tthis.dirtFactor++;\n\t\t\treturn weight\/=2;\n\t\t} else alert(myName+&quot; can&#39;t exercise, he&#39;s dead!&quot;);\n\t}&nbsp;\n\tthis.weigh=function(){ return weight }&nbsp;\n\tthis.getRace=function(){ return race }&nbsp;\n\tthis.getAge=function(){ return age }&nbsp;\n\tthis.muchTimePasses=function(){ age+=50; this.dirtFactor=10; }&nbsp;\n\n\n\t\/\/ ************************************************************************&nbsp;\n\t\/\/ PUBLIC PROPERTIES -- ANYONE MAY READ\/WRITE&nbsp;\n\t\/\/ ************************************************************************&nbsp;\n\tthis.clothing=&quot;nothing\/naked&quot;;\n\tthis.dirtFactor=0;\n}&nbsp;\n\n\n\/\/ ************************************************************************&nbsp;\n\/\/ PUBLIC METHODS -- ANYONE MAY READ\/WRITE&nbsp;\n\/\/ ************************************************************************&nbsp;\nPerson.prototype.beCool = function(){ this.clothing=&quot;khakis and black shirt&quot; }&nbsp;\nPerson.prototype.shower = function(){ this.dirtFactor=2 }&nbsp;\nPerson.prototype.showLegs = function(){ alert(this+&quot; has &quot;+this.legs+&quot; legs&quot;) }&nbsp;\nPerson.prototype.amputate = function(){ this.legs-- }&nbsp;\n\n\n\/\/ ************************************************************************&nbsp;\n\/\/ PROTOTYOPE PROERTIES -- ANYONE MAY READ\/WRITE (but may be overridden)&nbsp;\n\/\/ ************************************************************************&nbsp;\nPerson.prototype.legs=2;\n\n\n\/\/ ************************************************************************&nbsp;\n\/\/ STATIC PROPERTIES -- ANYONE MAY READ\/WRITE&nbsp;\n\/\/ ************************************************************************&nbsp;\nPerson.population = 0;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>well explained public, private and protected variables and methods in javascript: http:\/\/phrogz.net\/js\/classes\/OOPinJS.html function Person(n,race){&nbsp; this.constructor.population++; \/\/ ************************************************************************&nbsp; \/\/ PRIVATE VARIABLES AND FUNCTIONS&nbsp; \/\/ ONLY PRIVELEGED METHODS MAY VIEW\/EDIT\/INVOKE&nbsp; \/\/ ***********************************************************************&nbsp; var alive=true, age=1; var maxAge=70+Math.round(Math.random()*15)+Math.round(Math.random()*15); function makeOlder(){ return alive = (++age &lt;= maxAge) }&nbsp; var myName=n?n:&quot;John Doe&quot;; var weight=1; \/\/ ************************************************************************&nbsp; \/\/ PRIVILEGED METHODS&nbsp; \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-908","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=908"}],"version-history":[{"count":0,"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/908\/revisions"}],"wp:attachment":[{"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ndk.sytes.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}