JS 中的 public /private /priviliaged

Summary

  • private variables are declared with the 'var' keyword inside the object, and can only be accessed by private functions and privileged methods.
  • private functions are declared inline inside the object's constructor (or alternatively may be defined via var functionName=function(){...}) and may only be called by privileged methods (including the object's constructor).
  • privileged methods are declared with this.methodName=function(){...} and may invoked by code external to the object.
  • public properties are declared with this.variableName and may be read/written from outside the object.
  • public methods are defined by Classname.prototype.methodName = function(){...} and may be called from outside the object.
  • prototype properties are defined by Classname.prototype.propertyName = someValue
  • static properties are defined by Classname.propertyName = someValue

Example

In this example, a person's name and race are set at birth and may never be changed. When created, a person starts out at year 1 and a hidden maximum age is determined for that person. The person has a weight which is modified by eating (tripling their weight) or exercising (halfing it). Every time the person eats or exercises, they grow a year older. The person object has a publicly accessible 'clothing' property which anyone can modify, as well as a dirtFactor which can be modified manually (throwing dirt on or scrubbing it off), but which increases every time the person eats or exercises, and is reduced by the use of the shower() method.

js 代码
 
  1. function Person(n,race){     
  2.     this.constructor.population++;    
  3. // ************************************************************************     
  4. // PRIVATE VARIABLES AND FUNCTIONS     
  5. // ONLY PRIVELEGED METHODS MAY VIEW/EDIT/INVOKE     
  6. // ***********************************************************************     
  7. var alive=true, age=1;     
  8. var maxAge=70+Math.round(Math.random()*15)+Math.round(Math.random()*15);     
  9. function makeOlder(){   
  10.     return alive = (++age <= maxAge)   
  11. }    
  12. var myName=n?n:"John Doe";     
  13. var weight=1;     
  14. // ************************************************************************     
  15. // PRIVILEGED METHODS     
  16. // MAY BE INVOKED PUBLICLY AND MAY ACCESS PRIVATE ITEMS     
  17. // MAY NOT BE CHANGED; MAY BE REPLACED WITH PUBLIC FLAVORS     
  18. // ************************************************************************     
  19. this.toString=this.getName=function(){ return myName }     
  20. this.eat=function(){         
  21.     if (makeOlder()){  
  22.                this.dirtFactor++;  
  23.                return weight*=3;         
  24.     } else   
  25.     alert(myName+" can't eat, he's dead!");     
  26. }     
  27. this.exercise=function(){         
  28.     if (makeOlder()){             
  29.         this.dirtFactor++;  
  30.         return weight/=2;         
  31.     } else   
  32.     alert(myName+" can't exercise, he's dead!");     
  33. }     
  34. this.weigh=function(){ return weight }     
  35. this.getRace=function(){ return race }     
  36. this.getAge=function(){ return age }     
  37. this.muchTimePasses=function(){ age+=50; this.dirtFactor=10; }  
  38. // ************************************************************************     
  39. // PUBLIC PROPERTIES -- ANYONE MAY READ/WRITE     
  40. // ************************************************************************     
  41. this.clothing="nothing/naked";     
  42. this.dirtFactor=0;}  
  43. // ************************************************************************  
  44. // PUBLIC METHODS -- ANYONE MAY READ/WRITE  
  45. // ************************************************************************  
  46. Person.prototype.beCool = function(){ this.clothing="khakis and black shirt" }  
  47. Person.prototype.shower = function(){ this.dirtFactor=2 }  
  48. Person.prototype.showLegs = function(){ alert(this+" has "+this.legs+" legs") }  
  49. Person.prototype.amputate = function(){ this.legs-- }  
  50. // ************************************************************************  
  51. // PROTOTYOPE PROERTIES -- ANYONE MAY READ/WRITE (but may be overridden)  
  52. // ************************************************************************  
  53. Person.prototype.legs=2;  
  54. // ************************************************************************  
  55. // STATIC PROPERTIES -- ANYONE MAY READ/WRITE  
  56. // ************************************************************************  
  57. Person.population = 0;  
  58. // Here is the code that uses the Person classfunction   
  59. RunGavinsLife(){     
  60.     var gk=new Person("Gavin","caucasian");//New instance of the Person object created.     
  61.     var lk=new Person("Lisa","caucasian"); //New instance of the Person object created.     
  62.     alert("There are now "+Person.population+" people");     
  63.     gk.showLegs(); lk.showLegs();//Both share the common 'Person.prototype.legs' variable when looking at 'this.legs'     
  64.     gk.race = "hispanic";  //Sets a public variable, but does not overwrite private 'race' variable.     
  65.     alert(gk+"'s real race is "+gk.getRace());    //Returns 'caucasian' from private 'race' variable set at create time.     
  66.     gk.eat(); gk.eat(); gk.eat();                 //weight is 3...then 9...then 27     
  67.     alert(gk+" weighs "+gk.weigh()+" pounds and has a dirt factor of "+gk.dirtFactor);     
  68.     gk.exercise();                                //weight is now 13.5     
  69.     gk.beCool(); //clothing has been update to current fashionable levels     
  70.     gk.clothing="Pimp Outfit";                    //clothing is a public variable that can be updated to any funky value     
  71.     gk.shower();     
  72.     alert("Existing shower technology has gotten "+gk+" to a dirt factor of "+gk.dirtFactor);     
  73.     gk.muchTimePasses();                          //50 Years Pass     
  74.     Person.prototype.shower=function(){           //Shower technology improves for everyone         
  75.         this.dirtFactor=0;     
  76.     }     
  77.     gk.beCool=function(){                         //Gavin alone gets new fashion ideas         
  78.         this.clothing="tinfoil";     
  79.     };     
  80.     gk.beCool();   
  81.     gk.shower();     
  82.     alert("Fashionable "+gk+" at "       +gk.getAge()+" years old is now wearing "       +gk.clothing+" with dirt factor "       +gk.dirtFactor);     
  83.     gk.amputate();                                //Uses the prototype property and makes a public property     
  84.     gk.showLegs(); lk.showLegs();                 //Lisa still has the prototype property     
  85.     gk.muchTimePasses();                          //50 Years Pass...Gavin is now over 100 years old.     
  86.     gk.eat();                                     //Complains about extreme age, death, and inability to eat.  
  87. }  

你可能感兴趣的:(prototype,Access)