@alias¶
-
.. @alias::¶ - 语法
@alias <aliasNamepath>- 概述
The
@aliastag causes JSDoc to treat all references to a member as if the member had a different name. This tag is especially useful if you define a class within an inner function; in this case, you can use the@aliastag to tell JSDoc how the class is exposed in your app.While the
@aliastag may sound similar to the@nametag, these tags behave very differently. The@nametag tells JSDoc to ignore any code associated with the comment. For example, when JSDoc processes the following code, it ignores the fact that the comment forbaris attached to a function:/** * Bar function. * @name bar */ function foo() {}
The
@aliastag tells JSDoc to pretend that Member A is actually named Member B. For example, when JSDoc processes the following code, it recognizes thatfoois a function, then renamesfootobarin the documentation:/** * Bar function. * @alias bar */ function foo() {}
- 示例
Suppose you are using a class framework that expects you to pass in a constructor function when you define a class. You can use the
@aliastag to tell JSDoc how the class will be exposed in your app.In the following example, the
@aliastag tells JSDoc to treat the anonymous function as if it were the constructor for the class “trackr.CookieManager”. Within the function, JSDoc interprets thethiskeyword relative to trackr.CookieManager, so the “value” method has the namepath “trackr.CookieManager#value”.Klass('trackr.CookieManager', /** * @class * @alias trackr.CookieManager * @param {Object} kv */ function(kv) { /** The value. */ this.value = kv; } );
You can also use the
@aliastag with members that are created within an immediately invoked function expression (IIFE). The@aliastag tells JSDoc that these members are exposed outside of the IIFE’s scope./** @namespace */ var Apple = {}; (function(ns) { /** * @namespace * @alias Apple.Core */ var core = {}; /** Documented as Apple.Core.seed */ core.seed = function() {}; ns.Core = core; })(Apple);
For members that are defined within an object literal, you can use the
@aliastag as an alternative to the [@lends]lendstag.Using @alias for an object literal¶// Documenting objectA with @alias var objectA = (function() { /** * Documented as objectA * @alias objectA * @namespace */ var x = { /** * Documented as objectA.myProperty * @member */ myProperty: 'foo' }; return x; })(); // Documenting objectB with @lends /** * Documented as objectB * @namespace */ var objectB = (function() { /** @lends objectB */ var x = { /** * Documented as objectB.myProperty * @member */ myProperty: 'bar' }; return x; })();