0

Is it possible to make protected instance method of the superclass public in the subclass using JSDoc annotation? More specifically, I have a class which is inherited from goog.ui.Control. goog.ui.Control has a protected method setElementInternal. I'd like to make its access modifier public in the subclass to access this method from another class.

2 Answers2

0

Not directly.

You will need to add a new method in your inherit class that is public which calls the @protected method. Something like this should work.

/**
 * My public wrapper around a protected method
 * @param {Element} element Root element for this component
 */
my.namespace.Control.prototype.setElement = function(element) {
    this.setElementInternal(element);
}
Technetium
  • 5,902
  • 2
  • 43
  • 54
  • 1
    Thanks for the answer. Google Closure Library authors don't use \@public accessor annotation. Namely, if there is no \@public annotation, compiler treat it as public method, member etc. Also, I tried the \@public annotation, but this time compiler gives the following warning (error due to my compiler options) Overriding PROTECTED property of goog.ui.Component.prototype with PUBLIC property. – user2073036 Feb 14 '13 at 23:05
  • You are correct in that they don't require @public. I do in my own code to clarify my intent. I'll remove it from my answer to make my coding style less distracting. It sounds like you may be using a newer version of the compiler than me that prevents you from doing an `@override` trick. – Technetium Feb 15 '13 at 01:14
  • It's better to avoid using "@public" entirely, as it makes the compiler spit out a bunch of annotation warnings which can clutter up the build output if you're logging it or skimming it for errors/important information. – JoeDuncan Feb 19 '13 at 18:45
  • I believe it is not allowed to call protected methods from sub classes. – Peter StJ Mar 05 '13 at 08:58
  • @PeterStJ https://developers.google.com/closure/compiler/docs/js-for-compiler "A property marked 'protected' is accessible to [...] static methods and instance methods of any subclass of the class on which the property is defined." – Technetium Mar 05 '13 at 16:56
0

The answer is shown below.

"foo.js"

goog.provide('foo');

...

goog.inherits(foo,goog.ui.Control);

...

/** * @param {Element} element */

foo.prototype.setElementInternalEncap = function (element) {

goog.bind(this.setElementInternal, this, element);

};

"bar.js"

goog.provide('bar');

goog.require('foo');

...

fooReference.setElementInternalEncap(element);

  • This won't work. `goog.bind(...)` returns a function, and that returned function isn't being executed. This is essentially a no-op. Technically, you could replace your current `goog.bind(...)` statement with this instead: `goog.bind(this.setElementInternal, this, element)();`. However, that completely defeats the purpose of calling `goog.bind()` to begin with. – Technetium Feb 19 '13 at 22:01