【问题标题】:How to document Closures如何记录闭包
【发布时间】:2013-02-28 21:15:11
【问题描述】:

我正在尝试在一个名为 User 的类中记录功能,该类位于闭包内 - 我如何使用 JsDoc3 做到这一点?

这是我所拥有的:

/**
    @class User
    @classdesc This is the class that describes each user.
*/
(function($){

    var _defaults = {
        'first_name': '',
        'last_name': ''
    };

    /**
        @constructor
    */
    function User(options) {
        this.options = $.extend({}, _defaults, options);
    }

    /**
        @method
        @desc Returns the combined first name and last name as a string
        @returns {string}
    */
    User.prototype.getName() = function(){
        return this.options.first_name + this.options.last_name;
    };

    window.User = User;

}(jQuery));

【问题讨论】:

    标签: javascript documentation closures jsdoc


    【解决方案1】:

    我用这种方法取得了成功。 (添加到样板插件中,因此包含 MIT 许可注释)

    参见原型上@global + @class 和@global 的使用。 好像可以。

    代码复制如下: 请尽情享受并让它变得更好。

    /**
    * jQuery lightweight plugin boilerplate
    * Original author: @ajpiano
    * Further changes, comments: @addyosmani
    * Licensed under the MIT license
    */
    
    ;(function ( $, window, document, undefined ) {
    
    var pluginName = "Application",
        defaults = {
            propertyName: "value"
        };
    
    /**
     * @global
     * @class Application
     * @description MASTER:  Sets up and controls the application
     * 
     * @returns Object
     */
    function Application( element, options ) {
        this.element = element;
        this.options = $.extend( {}, defaults, options) ;
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
         window[pluginName] = this;
    }
    
    /** @global */
    Application.prototype = {
    
        /**
        * @description call pre-life initialisations and tests here
        */
        init: function() {
    
            var that = this;
           that._build();
           that._setNotifications();
        },
    
        /**
        @description Set up a pub sub for global notifications such a state-changes.
        */
        _setNotifications: function(el, options) {
            console.log('Application=>setNotifications()');
        },
    
    
        /**
        @description All environment setup done we now call other plugins.
        */
        _build: function(el, options) {
            console.log('Application=>build()');
        }
    };
    
    $.fn[pluginName] =  function ( options ) {
        return this.each(function () {
            if (!$.data(this, "plugin_" + pluginName)) {
                $.data(this, "plugin_" + pluginName,
                new Application( this, options ));
            }
        });
    };
    
    
    
    
    })( jQuery, window, document );
    

    【讨论】:

      【解决方案2】:

      我不知道为什么 jsdoc3 决定忽略闭包中的文档,但至少有一种解决方法是使用@memberOf 标签来明确告诉方法属于哪个类:

      /**
       * @memberOf User
       * Returns the combined first name and last name as a string
       * @returns {string}
       */
      User.prototype.getName = function(){
      

      另外需要注意的是,您不需要使用@desc@classdesc 标签——这些标签是由jsduc3 自己自动添加的,这些标签的存在更像是一个实现细节。

      【讨论】:

        【解决方案3】:

        在我的情况下,闭包在单个文件中,并使用字典导出其对象,因此在概述中使用 @file 和 @exports DicName 标记以及字典的 @namespace 标记可以使它们记录在案。

        /**
            @file myFileWithClosure
            @exports DicName
        */
        (function($){
        
            /** @namespace */
            DicName = {};
        
            ...
        

        不需要全局。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-27
          • 2011-12-25
          • 2022-12-30
          • 2010-10-12
          • 1970-01-01
          • 1970-01-01
          • 2017-01-08
          相关资源
          最近更新 更多