Advertisement
Guest User

JavaScript OOP

a guest
Jan 28th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Define a constructor for an 'SSAParser' object
  2. function SSAParser(text) {
  3.   this.text = text;
  4.   this.lines = [];
  5. }
  6.  
  7. // Define a function for our parser object. So that memory is only allocated for it once across many objects, we put it on the prototype.
  8. // Inside the prototype, 'this' refers to the instance object
  9. SSAParser.prototype = {
  10.   parse: function() {
  11.     return "SSA parser: " + this.text;
  12.   },
  13.  
  14.   getName: function() {
  15.     return "SSA parser";
  16.   }
  17. };
  18.  
  19. // Instantiate with 'new' so that the properties are put on a new object (called 'this' in the constructor)
  20. var parser = new SSAParser("1");
  21. var parsed = parser.parse();
  22.  
  23. // Not using 'new' will cause it to be like a normal function call.
  24. // The result will have 'this' point to the function itself, causing unpleasantness
  25. var parser = SSAParser("1");
  26.  
  27. try {
  28.   var parsed = parser.parse();  // SSAParser doesn't return a value, so 'var parser' is undefined, causing an error
  29. } catch (e) { alert("Please use 'new'"); }
  30.  
  31. // Being clever, we can avoid using 'new' by always calling the function directly, and explicitly specifying 'this' to be our parser object. This results in far more typing.
  32. var parser = {};
  33. SSAParser.call( parser, text );
  34. var parsed = SSAParser.prototype.parse.call(parser);
  35.  
  36. // Everything on the prototype is publicly accessible
  37. // Using closures, we can adapt the above method to have hidden helper functions that aren't publicly accessible
  38. SSAParser.prototype.useHelper = (function (){
  39.   // This function is defined once, but is not directly on the prototype.
  40.   // It is not publicly visible, but as it's defined in the same closure as a function on the prototype, it is accessible
  41.   // 'this' points to the 'getHelperText' function by default, unless we override it
  42.   function getHelperText() {
  43.     return "From hidden helper: " + this.text;
  44.   }
  45.  
  46.   // This function ends up on the prototype, we can access 'this' properly from within it
  47.   return function() {
  48.     var text = "From helper: " + this.text;
  49.    
  50.     // We can call getHelperText and reference our instance members within.
  51.     // It becomes a private member function
  52.     text += getHelperText.call(this);
  53.     return this.text;
  54.   }
  55. })();
  56.  
  57.  
  58. // Inheritance, ASSParser extends SSAParser
  59. function ASSParser(text) {
  60.   // Explicitly call parent constructor
  61.   SSAParser.call( this, text );
  62. }
  63.  
  64. // We set the prototype to be our base class and update the constructor
  65. ASSParser.prototype = new SSAParser();
  66. ASSParser.prototype.constructor = SSAParser;
  67.  
  68. // Override parent definition of getName
  69. // Unless we store the old definition of 'getName' we can no longer call the parent version
  70. ASSParser.prototype.getName = function() {
  71.     return "ASS parser";
  72. }
  73.  
  74. // Define new function, which references parent
  75. ASSParser.prototype.getPi = function() {
  76.     return Math.PI;
  77. }
  78.  
  79. var parser = new ASSParser("sample");
  80. alert(parser.getName());
  81. alert(parser.getPi());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement