Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 2.59 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Javascript Prototype Chaining super class constructor and method calling
  2. //class parent
  3. function parent(param_1){
  4.     this.param = param_1;
  5.     this.getObjWithParam = function(val){
  6.         console.log("value in parent class "+val);
  7.         console.log("Constructor parameter : "+this.param);
  8.     };
  9.  
  10. };
  11.  
  12. //class child
  13. function child(param_1){
  14.     this.constructor(param_1);
  15.     this.getObjWithParam = function(val){
  16.         console.log("value in child class "+val);
  17.         val = Number(val)+1;
  18.         child.prototype.getObjWithParam.call(this, [val]);
  19.     };
  20. };
  21. child.prototype = new parent();
  22.  
  23. //class grandChild
  24. function grandChild(param_1){
  25.     this.constructor(param_1);
  26. };
  27. grandChild.prototype = new child();
  28.  
  29.  
  30. var gc = new grandChild(666);
  31. gc.getObjWithParam(0);
  32.        
  33. value in parent class 0
  34. Constructor parameter : 666
  35.        
  36. console.log(gc.constructor)
  37.        
  38. function parent(param_1){
  39.     this.param = param_1;
  40.     this.getObjWithParam = function(val){
  41.         console.log("value in parent class "+val);
  42.         console.log("Constructor parameter : "+this.param);
  43.     };
  44. }
  45.        
  46. //class parent
  47. function parent(param_1){
  48.     console.log("parent " + param_1);
  49.     this.param = param_1;
  50. }
  51.  
  52. parent.prototype.getObjWithParam = function(val) {
  53.     console.log("value in parent class "+val);
  54.     console.log("Constructor parameter : "+this.param);
  55. };
  56.  
  57. //class child
  58. function child(param_1){
  59.     console.log("child " + param_1);
  60.     this.constructor(param_1);
  61. }
  62. child.prototype = new parent();
  63. child.prototype.getObjWithParam = function(val) {
  64.     console.log("value in child class "+val);
  65.     val = Number(val)+1;
  66.     parent.prototype.getObjWithParam.call(this, [val]);    
  67. }
  68.  
  69. //class grandChild
  70. function grandChild(param_1){
  71.     console.log("grandChild " + param_1);
  72.     this.constructor(param_1);
  73. }
  74. grandChild.prototype = new child();
  75.  
  76.  
  77. var gc = new grandChild(666);
  78. gc.getObjWithParam(0);
  79.        
  80. <div id="div"></div>
  81.  
  82. <script type="text/javascript">
  83. function $(id) {
  84.     if(this.$) return new $.init(id);
  85. }
  86.  
  87. $.init = function(id) {
  88.      if(typeof id == 'string') {
  89.          this.id = document.getElementById(id);
  90.      }
  91. };
  92.  
  93. $.init.prototype = $.prototype = {
  94.     constructor: $,
  95.     css: function(value) {
  96.         for(i in value) {
  97.             this.id.style[i] = value[i];
  98.         }
  99.         return this;
  100.     },
  101.     mush : function() {
  102.         var text = this.id.innerHTML;
  103.         this.id.innerHTML = text.split('').join('--');
  104.         return this;
  105.     },
  106.     text : function(a) {
  107.         this.id.innerHTML = a;
  108.         return this;
  109.     }
  110. };
  111.  
  112. $('div').text('FOO').mush().css({
  113.         'color' : 'red',
  114.         'textTransform' : 'uppercase'
  115. });
  116. </script>