In this snippet of Google Closure javascript code involving a constructor, why is goog.base(this); necessary? Doesn’t Foo already inherit from Disposable with goog.inherits(foo, goog.Disposable);?
goog.provide('Foo');
/**
* @constructor
* @extends {goog.Disposable}
*/
Foo = function() {
goog.base(this);
}
goog.inherits(foo, goog.Disposable);
foo.prototype.doSomething = function(){
...
}
foo.prototype.disposeInternal = function(){
...
}
goog.inherits(childConstructor, parentConstructor)
goog.inherits()establishes the prototype chain from the child constructorto the parent constructor.
In addition to prototype properties, constructors may have “own” properties
(i.e. instance-specific properties added to
this). Sincegoog.inherits()does not call the parent constructor, own properties are not copied to the
child constructor and any initialization code in the parent does not get
executed. For these reasons, the standard pattern is to chain constructors as in the following example.
goog.base(self, opt_methodName, var_args)
goog.base()is a helper function for calling parent methods so that you donot need to explicitly use call() or apply().
In Closure code it is common to chain constructors with
goog.base()ratherthan calling the parent constructor explicitly.
Further Reading
goog.inherits()– Classical Pattern #5–A Temporary Constructor)