Guest User

Untitled

a guest
May 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. var MyNamedThing = Class.create({
  2. initialize: function(name) {
  3. // The 'name' parameter is a private variable, no one can see it from the outside
  4. // Here's another private variable:
  5. var description;
  6.  
  7. // 'description' is the same as 'name' unless the caller sets one explicitly
  8. // We do a fairly silly implementation of that to make the point about private vars and accessors
  9. description = name;
  10.  
  11. // Public accessors for 'name'
  12. this.getName = function() {
  13. return name;
  14. };
  15. this.setName = function(newname) {
  16. if (!newname) {
  17. throw "Invalid name";
  18. }
  19. if (description == name) {
  20. description = newname;
  21. }
  22. name = newname;
  23. };
  24.  
  25. // Public accessors for 'description'
  26. this.getDescription = function() {
  27. return description;
  28. };
  29. this.setDescription = function(newdesc) {
  30. if (!newdesc) {
  31. throw "Invalid description";
  32. }
  33. description = newdesc;
  34. };
  35. }
  36. });
Add Comment
Please, Sign In to add comment