How to have a type in Closure Compiler externs without a constructor

雨燕双飞 提交于 2019-12-24 15:58:34

问题


I am trying to make externs for the Google Closure Compiler for types that have no constructor.

I have tried the following, but it gives me a Bad type annotation. Unknown type WindowsMediaActiveX.Cdrom error because nothing tells the compiler that WindowsMediaActiveX.Cdrom is a type instead of just a collection of methods/properties.

/**
 * @fileoverview Declares externs for the Windows media player ActiveX control.
 * @author Joshua Dwire
 * @suppress {duplicate}
 */

var WindowsMediaActiveX={};


/**
 * Methods and properties for accessing a CD or DVD in its drive.
 */
WindowsMediaActiveX.Cdrom={};


/**
 * Retrieves the CD or DVD drive letter.
 * @type {string}
 * @readonly
 */
WindowsMediaActiveX.Cdrom.prototype.driveSpecifier;



/**
 * Methods and properties for accessing a collection of CD or DVD drives.
 */
WindowsMediaActiveX.CdromCollection={};


/**
 * Retrieves the Cdrom object associated with a particular drive letter.
 * @param {string} driveSpecifier String containing the drive letter followed by a colon (":") character.
 * @returns {WindowsMediaActiveX.Cdrom}
 */
WindowsMediaActiveX.CdromCollection.prototype.getByDriveSpecifier=function(driveSpecifier){};

I know I could change:

/**
 * Methods and properties for accessing a CD or DVD in its drive.
 */
WindowsMediaActiveX.Cdrom={};

to:

/**
 * Methods and properties for accessing a CD or DVD in its drive.
 * @constructor
 */
WindowsMediaActiveX.Cdrom=function(){};

but then the compiler wouldn't show a warning if I or someone else tried to use new WindowsMediaActiveX.Cdrom(). Any ideas on how to define this?

For those of you who want more information, I am working on a media player that will use the Windows Media ActiveX Control to play media. I am also using the Google Closure Compiler and Library. I need to define the externs for the player to work correctly, but none of the types used by the ActiveX Control have constructors. They are all created through other methods or through creating an object in the html. How should I define this in the extern file? Thanks for your help.


回答1:


The typical annotation for this pattern is:

/** @const */
var WindowsMediaActiveX = {};

/**
 * Methods and properties for accessing a CD or DVD in its drive.
 * @constructor
 * @private
 */
WindowsMediaActiveX.Cdrom=function(){};

The @private annotation is the indication that the constructor is not meant to be directly called. However, the compiler will only report a warning on direct instantiations of the type when the accessControls warning group is enabled (on with VERBOSE warnings).

Edit: Answer updated to add the required @const annotation to the namespace. The access control annotations will be ignored otherwise.



来源:https://stackoverflow.com/questions/13187909/how-to-have-a-type-in-closure-compiler-externs-without-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!