Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. // Class.extend takes a function that returns the options object
  2. // it is passed the accessor function generated in the Pvt.js example above
  3. var Person = Class.extend(function(pvt) {
  4.  
  5. // private static member
  6. var species = "Homo sapien";
  7.  
  8. return {
  9. init: function(isDancing) {
  10. // pvt(this).invisible === undefined
  11. // pvt(this).hasSword === undefined
  12.  
  13. pvt(this).dancing = isDancing;
  14. // pvt(this).dancing === isDancing
  15. },
  16. dance: function() {
  17. return pvt(this).dancing;
  18. },
  19. getSpecies: function() {
  20. return species ;
  21. }
  22. };
  23. });
  24.  
  25. var Ninja = Person.extend(function(pvt) {
  26. return {
  27. init: function(hasSword){
  28. pvt(this).invisible = true;
  29. this._super( false );
  30. pvt(this).hasSword = hasSword || true;
  31.  
  32. // pvt(this).dancing === false
  33. },
  34. dance: function(){
  35. // Call the inherited version of dance()
  36. return this._super();
  37. },
  38. swingSword: function(){
  39. return pvt(this).hasSword;
  40. }
  41. };
  42. });
  43.  
  44.  
  45. var p = new Person(true);
  46. p.dance(); // => true
  47.  
  48. var n = new Ninja();
  49. n.dance(); // => false
  50. n.swingSword(); // => true
  51.  
  52. // Should all be true
  53. p instanceof Person && p instanceof Class &&
  54. n instanceof Ninja && n instanceof Person && n instanceof Class;
Add Comment
Please, Sign In to add comment