为什么除了`goog.inherits()`之外还需要`goog.base(this)`? [英] Why is `goog.base(this)` necessary in addition to `goog.inherits()`?

查看:166
本文介绍了为什么除了`goog.inherits()`之外还需要`goog.base(this)`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在涉及构造函数的Google Closure javascript代码的这段代码中,为什么goog.base(this);是必需的? Foo是否已经通过goog.inherits(foo, goog.Disposable);从Disposable继承了?

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()通过子构造函数建立原型链 到父构造函数.

goog.inherits(childConstructor, parentConstructor)

goog.inherits() establishes the prototype chain from the child constructor to the parent constructor.

/**
 * Inherit the prototype methods from one constructor into another.
 * @param {Function} childCtor Child class.
 * @param {Function} parentCtor Parent class.
 */
goog.inherits = function(childCtor, parentCtor) {
  /** @constructor */
  function tempCtor() {};
  tempCtor.prototype = parentCtor.prototype;
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new tempCtor();
  /** @override */
  childCtor.prototype.constructor = childCtor;
};


除了原型属性外,构造函数还可以具有自己的"属性 (即,添加到this的特定于实例的属性).自goog.inherits() 不调用父构造函数,自己的属性不会复制到 子构造函数和父中的任何初始化代码均未获得 被执行.由于这些原因,标准模式是链构造器如以下示例所示.


In addition to prototype properties, constructors may have "own" properties (i.e. instance-specific properties added to this). Since goog.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.

/**
 * @param {string} name The parent's name.
 * @constructor
 */
var Parent = function(name) {
  /**
   * @type {string}
   * @private
   */
  this.name_ = name;
}

/**
 * @param {string} name The child's name.
 * @constructor
 * @extends {Parent}
 */
var Child = function(name) {
  Parent.call(this, name);
}
goog.inherits(Child, Parent);


goog.base()是用于调用父方法的帮助程序函数,因此您可以 无需显式使用 call() apply().

goog.base() is a helper function for calling parent methods so that you do not need to explicitly use call() or apply().

如果从构造函数中调用[goog.base()],则此调用 具有参数1-N的超类构造函数.

If [goog.base()] is called from a constructor, then this calls the superclass contructor with arguments 1-N.

如果这是从原型方法中调用的,则必须传递名称 该方法作为此函数的第二个参数.如果你不, 您将收到运行时错误.这将调用超类的方法 参数2-N.

If this is called from a prototype method, then you must pass the name of the method as the second argument to this function. If you do not, you will get a runtime error. This calls the superclass' method with arguments 2-N.

此功能仅在使用goog.inherits表示继承时有效 您的班级之间的关系.

This function only works if you use goog.inherits to express inheritance relationships between your classes.

在闭包代码中,通常使用goog.base()链接构造函数,而不是 而不是显式调用父构造函数.

In Closure code it is common to chain constructors with goog.base() rather than calling the parent constructor explicitly.

/**
 * @param {string} name The child's name.
 * @constructor
 * @extends {Parent}
 */
var Child = function(name) {
  goog.base(this, name);
}
goog.inherits(Child, Parent);


  • Inheritance Patterns in JavaScript by Michael Bolin
  • An Examination of goog.base() by Michael Bolin
  • Closure: The Definitive Guide by Michael Bolin
  • JavaScript Patterns by Stoyan Stefanov (see Chapter 6: "Code Reuse Patterns" for a detailed analysis of the pattern used to implement goog.inherits() - Classical Pattern #5--A Temporary Constructor)

这篇关于为什么除了`goog.inherits()`之外还需要`goog.base(this)`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆