- Javascript Prototype Chaining super class constructor and method calling
- //class parent
- function parent(param_1){
- this.param = param_1;
- this.getObjWithParam = function(val){
- console.log("value in parent class "+val);
- console.log("Constructor parameter : "+this.param);
- };
- };
- //class child
- function child(param_1){
- this.constructor(param_1);
- this.getObjWithParam = function(val){
- console.log("value in child class "+val);
- val = Number(val)+1;
- child.prototype.getObjWithParam.call(this, [val]);
- };
- };
- child.prototype = new parent();
- //class grandChild
- function grandChild(param_1){
- this.constructor(param_1);
- };
- grandChild.prototype = new child();
- var gc = new grandChild(666);
- gc.getObjWithParam(0);
- value in parent class 0
- Constructor parameter : 666
- console.log(gc.constructor)
- function parent(param_1){
- this.param = param_1;
- this.getObjWithParam = function(val){
- console.log("value in parent class "+val);
- console.log("Constructor parameter : "+this.param);
- };
- }
- //class parent
- function parent(param_1){
- console.log("parent " + param_1);
- this.param = param_1;
- }
- parent.prototype.getObjWithParam = function(val) {
- console.log("value in parent class "+val);
- console.log("Constructor parameter : "+this.param);
- };
- //class child
- function child(param_1){
- console.log("child " + param_1);
- this.constructor(param_1);
- }
- child.prototype = new parent();
- child.prototype.getObjWithParam = function(val) {
- console.log("value in child class "+val);
- val = Number(val)+1;
- parent.prototype.getObjWithParam.call(this, [val]);
- }
- //class grandChild
- function grandChild(param_1){
- console.log("grandChild " + param_1);
- this.constructor(param_1);
- }
- grandChild.prototype = new child();
- var gc = new grandChild(666);
- gc.getObjWithParam(0);
- <div id="div"></div>
- <script type="text/javascript">
- function $(id) {
- if(this.$) return new $.init(id);
- }
- $.init = function(id) {
- if(typeof id == 'string') {
- this.id = document.getElementById(id);
- }
- };
- $.init.prototype = $.prototype = {
- constructor: $,
- css: function(value) {
- for(i in value) {
- this.id.style[i] = value[i];
- }
- return this;
- },
- mush : function() {
- var text = this.id.innerHTML;
- this.id.innerHTML = text.split('').join('--');
- return this;
- },
- text : function(a) {
- this.id.innerHTML = a;
- return this;
- }
- };
- $('div').text('FOO').mush().css({
- 'color' : 'red',
- 'textTransform' : 'uppercase'
- });
- </script>