何时在闭包中使用typedef? [英] When to use typedef in closure?

查看:81
本文介绍了何时在闭包中使用typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码片段,我已经很困惑了:

I have been confused for a long time regarding the following code snippet:

/**
 * Pair of width and height.
 * @param {string} width
 * @param {string} height
 * @constructor
 * @struct
 */
var Pair = function(width, height) {
  /** @type {string} */
  this.key = key;
  /** @type {string} */
  this.value = value;
};

/**
 * @typedef {{width: string, height: string}}
 */
var Pair;

基本上我需要创建一种新类型,并且对于何时使用哪种类型感到非常困惑?

Basically I need to create a new type and highly confused about which one to use when?

推荐答案

何时使用哪个?

which one to use when?

在某种程度上,这是个人喜好和编码风格的问题. Closure Compiler更倾向于Java中具有类,接口,方法等的伪经典继承模式,这与其他方法不同.请参阅Michael Bolin的 JavaScript中的继承模式.

This is to some extent a matter of personal preference and coding style. Closure Compiler leans more towards the pseudoclassical inheritance pattern found in Java with classes, interfaces, methods, etc. which is different to other approaches. See Inheritance Patterns in JavaScript by Michael Bolin.

另请参见为Closure Compiler注释JavaScript ,其中包含各种标签的最简洁的定义.

See also Annotating JavaScript for the Closure Compiler which has the most concise definitions of the various tags.

对于我自己,我使用伪古典继承模式.因此,我有99%的时间用@constructor定义要与new关键字一起使用的类.这些类也用@struct标记,因此它们具有一组固定的属性和方法.大约80%的类声明它们@implement@interface是因为这使我可以使用接口类型,而不必绑定到特定的类实现.

For myself, I use the pseudoclassical inheritance pattern. Therefore 99% of the time I am defining classes with @constructor which are meant to be used with the new keyword. These classes are also marked with @struct so they have a fixed set of properties and methods. Around 80% of my classes state that they @implement an @interface because that allows me to use the interface types and not be tied to a particular class implementation.

有时,我将使用@typedef定义一个具有一些最小属性集的对象.对我来说,这种类型的对象通常没有定义任何函数,这只是聚合某些属性的一种方式.

Occasionally I will use @typedef to define an object that has some minimal set of properties. For me, this type of object usually has no functions defined on it, it is merely a way to aggregate some properties.

这是我代码中的一个示例:

Here's an example from my code:

/**  A regular expression and the replacement string expression to be used in a
* `String.replace()` command.
* @typedef {{regex: !RegExp, replace: string}}
* @private
*/
Terminal.regexPair;

我可以在其他地方使用该typedef:

I can use that typedef elsewhere:

/** Set of regular expressions to apply to each command.
* @type {!Array.<!Terminal.regexPair>}
* @private
*/
this.regexs_ = [];

优点是很容易制作这种类型的东西:

The advantage is that it is easy to make something of this type:

/** Add regular expression and replacement string to be used by `String.replace()`
* for transforming script commands before they are executed.
* @param {!RegExp} regex regular expression for finding a name to transform
* @param {string} replace the replacement string for use with `String.replace()`
*/
Terminal.prototype.addRegex = function(regex, replace) {
  this.regexs_.push({regex:regex, replace:replace});
};

请注意,我使用{regex:regex, replace:replace}而不是使用new运算符创建了一个匿名对象.但是,编译器正在检查此对象是否具有由Terminal.regexPair定义的必需(最小)属性集.

Notice that I made an anonymous object with {regex:regex, replace:replace} instead of using the new operator. Yet the compiler is checking that this object has the required (minimal) set of properties defined by Terminal.regexPair.

但是关于如何编写JavaScript代码还有许多其他哲学.而且您当然可以定义一个typedef,它需要具有指定签名的函数.

But there are many other philosophies about how to write JavaScript code. And you could certainly define a typedef that requires functions with specified signatures.

这篇关于何时在闭包中使用typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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