oop - Calling the public instance methods of a composed private class member without passthrough functions in Javascript -


i trying find way avoid creating lots of passthrough methods on class's prototype. have class progressbar, has lot of instance methods. want create new class (called composedprogressbar in code example), "has a" progressbar instance, , not inherit progressbar.

in order access progressbar's instance methods client code, customary create series of passthrough functions. such as:

composedprogressbar.prototype.setwidth = function (width) {     this.progressbar.setwidth(width); }; 

however, i'm trying avoid this.

i able access progressbar's privileged methods adding following composedprogressbar's constructor:

progressbar.call(this); 

but, isn't suitable i'm trying implement. need access methods have been added progressbar's prototype.

below example code based on i'm working with. have included height setter , getter illustrate using progressbar.call(this) works them.

is possible i'm trying achieve?

function progressbar() {     "use strict";     this.width = 0;     this.height = 0;      this.setheight = function (height) {         this.height = height;     };      this.getheight = function () {         return this.height;     }; }  progressbar.prototype.setwidth = function (width) {     "use strict";     this.width = width; };   progressbar.prototype.getwidth = function () {     "use strict";     return this.width; };  function composedprogressbar() {     "use strict";     this.progressbar = new progressbar();     progressbar.call(this); }   var composedprogressbar = new composedprogressbar();  composedprogressbar.setheight(300); console.log(composedprogressbar.getheight()); composedprogressbar.setwidth(300); console.log(composedprogressbar.getwidth()); 

i suppose write like:

for (var methodname in progressbar.prototype) {     if (typeof progressbar.prototype[methodname] === 'function'             && progressbar.prototype[methodname]                    !== composedprogressbar.prototype[methodname]) {         composedprogressbar.prototype[methodname] = (function (methodname) {             return function () {                 return this.progressbar[methodname]                            .apply(this.progressbar, arguments);             };         })(methodname);     } } 

(naturally create delegates methods exist in progressbar.prototype: wouldn't detect methods added later, , wouldn't support ad-hoc method-ing of apply.)


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -