简体   繁体   English

goog.inherits使用临时构造函数的优点是什么?

[英]What's the advantage of goog.inherits' use of a temporary constructor?

From Google's Closure library: 来自Google的Closure库:

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;
};

What is the advantage to the temporary constructor that is created? 创建的临时构造函数有什么优势?

Is there a reason the code doesn't just look like this: 是否有一个原因代码不是这样的:

goog.inherits = function(childCtor, parentCtor) {
  /** @constructor */
  childCtor.superClass_ = parentCtor.prototype;
  childCtor.prototype = new parentCtor();
  /** @override */
  childCtor.prototype.constructor = childCtor;
};

The first snippet is not calling the parentCtor - it is not instantiating an object with invoking the constructor on it, it just inherits from the parentCtor.prototype - actually it's a workaround for Object.create (very old browsers lack support of it). 第一个片段没有调用parentCtor - 它不是通过调用构造函数来实例化一个对象,它只是从parentCtor.prototype继承 - 实际上它是Object.create的一种解决方法(很老的浏览器缺乏对它的支持)。 See also Understanding Crockford's Object.create shim on how the tempCtor works and What is the reason [not] to use the 'new' keyword here? 又见了解克罗克福德的垫片的Object.create对如何tempCtor工作以及是什么原因[不]在这里使用了“新”的关键字? on the undesirability of calling the parent. 关于呼叫父母的不受欢迎程度。

You can only use "new parentCtor" if: (a) it will succeed without any parameters (b) you want the values set on the "this" value in parentCtor on your prototype. 如果出现以下情况,您只能使用“new parentCtor”:(a)它将在没有任何参数的情况下成功(b)您希望在原型上的parentCtor中的“this”值上设置值。

You see people do this in simple cases: 你看到人们在简单的情况下这样做:

var C = function() {};
C.prototype = new P();

But you can see how this can fail if P is: 但是你可以看到如果P是这样的话会失败:

var P = function(a) {a.x()}  // throws if "a" is undefined.

The tempCtor avoids this. tempCtor避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 goog.inherits存在于输出文件中 - goog.inherits present in the output file 除了`goog.inherits()`之外,为什么`goog.base(this)`是必要的? - Why is `goog.base(this)` necessary in addition to `goog.inherits()`? Google停业:Goog.provide和Goog.require有什么区别 - Google-closure : What's the difference between Goog.provide vs Goog.require 检查对象是否立即从构造函数继承 - Check if an object's immediately inherits from constructor 它继承自字符串的原型是什么? - What is a string's prototype it inherits from? Javascript原型 - 在这种情况下有什么优势? - Javascript prototypes - what's the advantage in this case? 使用此JavaScript编码模式定义构造函数有什么好处? - What is the advantage of using this JavaScript coding pattern to define constructor functions? 当存在“ goog.object”时,对“ goog.structs”有什么需求? - What is the need of 'goog.structs' when 'goog.object' is there? 将JavaScript字符串映射到“常量”有什么好处? - What's the advantage of mapping a JavaScript string to a “constant”? 将别名绑定到本地方法有什么好处? - What's the advantage of binding an alias to a local method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM