javascript - JSDoc 记录原型(prototype)继承

标签 javascript prototype jsdoc

假设我们正在构建一个嵌套的命名空间,我们将在其中根据需要对多个对象进行原型(prototype)设计。默认情况下,对象 window.blah.foo 是空的。然后,我们在一个原型(prototype)中对所有子对象及其功能进行原型(prototype)设计。当如下记录时,我们将所有内部函数显示为全局函数。现在假设您有数十个以相同方式构建的文件。我们如何记录这一点,以便 .bar1()、bar2() 和 bar3() 函数成为 window.blah.foo 对象的方法,特别是当它的实例化发生在该文件外部时,可能会多次。

/**
 * Blah - Our very own global Namespace
 * @namespace
 * @type {object}
 * @global
 * @public
*/
window.blah = window.blah || {};

/**
 * Immediately-Invoked Function Expression (IIFE). 
 * @function
 * @param {object} w - Global window object.
 * @returns {Object} window.blah.foo
*/
(function (w) {

    // strict JS
    'use strict';

    /**
     * Create instance of w.blah.foo constructor
     * @constructor
    */
    w.blah.foo = function () {

   };

    /**
     * Append properties and methods to an instance of window.blah.foo
     * @constructor
    */
    w.blah.foo.prototype = {

        /**
         * Dummy function to return the number 1.
         * @method bar1
        */
        bar1: function () {
            console.log(1);
        },

        /**
         * Dummy function to return the number 2.
         * @method bar2
        */
        bar2: function () {
            console.log(2);
        },

        /**
         * Dummy function to return the number 3.
         * @method bar3
        */
        bar3: function () {
            console.log(3);
        }

    };

}(window));

最佳答案

以下代码会将 barX 方法移动到它们所属的位置。请注意,jsdoc 与记录 IIFE 的 doclet 没有任何关系。将方法放在正确位置的关键是 /** @lends blah.foo# */。请参阅 @lends 的文档。 # 告诉 jsdoc 借给 blah.foo 的项目属于该类的实例。请参阅 namepaths 上的文档了解更多详情。

/**
 * Blah - Our very own global Namespace
 * @namespace
 * @global
 */
window.blah = window.blah || {};

/**
 * Immediately-Invoked Function Expression (IIFE).
 * @function
 * @param {object} w - Global window object.
 * @returns {Object} window.blah.foo
 */
(function (w) {
    // strict JS
    'use strict';

    /**
     * Create instance of w.blah.foo constructor
     * @constructor
     * @name blah.foo
     */
    w.blah.foo = function () {

    };

    /** @lends blah.foo# */
    w.blah.foo.prototype = {

        /**
         * Dummy function to return the number 1.
         */
        bar1: function () {
            console.log(1);
        },

        /**
         * Dummy function to return the number 2.
         */
        bar2: function () {
            console.log(2);
        },

        /**
         * Dummy function to return the number 3.
         */
        bar3: function () {
            console.log(3);
        }

    };

}(window));

关于javascript - JSDoc 记录原型(prototype)继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943599/

相关文章:

javascript - 如何指示对外部模块的依赖?

javascript - 了解具有事件元素的菜单背后的逻辑

javascript - 计算自动滚动的div高度

javascript - 行尾没有元素符号的元素符号分隔段落

javascript - 错误 "Uncaught SyntaxError: Unexpected token with JSON.parse"

JavaScript:扩充对象/函数原型(prototype)避免名称冲突

javascript - 遍历 NodeList : Array. prototype.forEach.call() vs Array.from().forEach

javascript - JavaScript 中的原型(prototype)继承 : I don't usually need calling the constructor of Parent object assigned to Child. 原型(prototype)

javascript - 为什么 JavaScript 函数文档位于函数之外?

javascript - 使用 Google Closure 的 @typedef 标签