JSDoc3使用路径名

原文:http://usejsdoc.org/about-namepaths.html

在JSDoc3中的路径名

如果涉及到一个JavaScript变量指的是在你的文档中的其他地方,你必须提供映射到该变量的唯一标识符。一个路径名提供了一种这样做的方法,和实例成员,静态成员和内部变量之间的歧义。

在JSDoc3中路径名基本的语法示例:

myFunction
MyConstructor
MyConstructor#instanceMember
MyConstructor.staticMember
MyConstructor~innerMember // note that JSDoc 2 uses a dash

下面给出了例子:一个被命名为“say”的实例方法,名为“say”内部function,和静态方法也被命名为“say”。这三种不同的方法,这些是所有彼此独立地存在的。

使用文件标记来描述你的代码。

/** @constructor */
Person = function() {
    this.say = function() {
        return "I'm an instance.";
    }

    function say() {
        return "I'm inner.";
    }
}
Person.say = function() {
    return "I'm static.";
}

var p = new Person();
p.say();      // I'm an instance.
Person.say(); // I'm static.
// there is no way to directly access the inner function from here

你可以使用三种不同的路径名语法来表示三种不同的方法:

使用文件标记来描述你的代码。

Person#say  // the instance method named "say."
Person.say  // the static method named "say."
Person~say  // the inner method named "say."

你可能惊讶为什么有一种语法涉及到一个内部方法当这种方法不能直接从它被定义的函数外部访问,虽然这是真实的,这个“〜”语法很少使用,有可能返回一个引用到一个内部方法从另一种方法的内部容器中,因此它可能在你的代码的其他地方的一些对象可能调用一个内部方法。

需要注意的是,如果一个构造函数有一个实例成员,这也是一个构造器,你可以简单地链路径名在一起,形成一个较长名路径名:

使用文件标记来描述你的代码。

/** @constructor */
Person = function() {
    /** @constructor */
    this.Idea = function() {
        this.consider = function(){
            return "hmmm";
        }
    }
}

var p = new Person();
var i = new p.Idea();
i.consider();

在这种情况下,引用命名方法“consider,”你会用下列名路径名:Person#Idea#consider

这个链接可与连接元件的任意组合使用:# . 〜

特殊情况:模块,外部组件和事件。

/** A module. Its name is module:foo/bar.
 * @module foo/bar
 */
/** The built in string object. Its name is external:String.
 * @external String
 */
/** An event. Its name is module:foo/bar.event:MyEvent.
 * @event module:foo/bar.event:MyEvent
 */

有一些特殊的情况用路径名:@module名称由前缀“module:”,@external名称由前缀“external:”,和@event名称由前缀“event:”。

在名称中,路径名对象带有特殊字符。

/** @namespace */
var chat = {
    /**
     * Refer to this by {@link chat."#channel"}.
     * @namespace
     */
    "#channel": {
        /**
         * Refer to this by {@link chat."#channel".open}.
         * @type {boolean}
         * @defaultvalue
         */
        open: true,
        /**
         * Internal quotes have to be escaped by backslash. This is
         * {@link chat."#channel"."say-\"hello\""}.
         */
        'say-"hello"': function (msg) {}
    }
};

/**
 * Now we define an event in our {@link chat."#channel"} namespace.
 * @event chat."#channel"."op:announce-motd"
 */

以上是一个命名空间的例子带有“unusual”的字符在它的成员名称中(#号,破折号,引号)。参考这些,你只需要引用的名字:chat.“#channel”chat.“#channel”.“op:announce-motd”,等等。在名称内部的引号应该转义为反斜杠:chat.”#channel”.”say-\”hello\”“。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值