intellisense.annotate 带有动态创建注释的函数

intellisense.annotate a function with a dynamically created annotation

我有一个函数 $.publish 通过将第一个参数绑定到 this 来发布函数列表,我打算用它来编写 X.prototype,例如:

X.prototype = (function () {
    function privateFunction(me) {
        /// <summary>privateFunction</summary>
        /// <param name="me" type="X"></param>
    }
    return $.publish({
        publicFunction: function (me, a) {
            /// <summary>publicFunction</summary>
            /// <param name="me" type="X"></param>
            /// <param name="a" type="Number"></param>
        }
    })
})();

所以我可以像

一样使用X
var x = new X;
x.publicFunction(0);

文件lib.js$.publish的实现是

publish: function (fs) {
    return $.map(fs, function () {
        var f = this;
        return function () {
            f.apply(/* irrelevant */);
        }
    })
}

.

问题

当我输入 x.publicFunction( 时,签名帮助只告诉我 publicFunction(),不知道参数 a.

MSDN doc - Adding IntelliSense Annotations 说我们可以像这样注释一个函数:

intellisense.annotate(someFunc, function (a) {
    /// <signature>
    /// <summary>Description of someFunc</summary>
    /// <param name="a">Param a</param>
    /// </signature>
});

所以我在lib.intellisense.js中写了如下代码:

$.publish = function (fs) {
    /// <summary>Publish a list of functions</summary>
    /// <param name="fs" type="Object">List of functions to publish</param>

    return $.map(fs, function () {
        var f = this;

        var published = function () { };
        //intellisense.redirectDefinition(published, f);

        var funcSrc = f.toString();
        var vsdocPattern = /^([ \t]+)(\/{3} .*)/mg;
        var indent = -1;
        var vsdoc = "";
        for (var doc; (doc = vsdocPattern.exec(funcSrc)) != null;) {
            if (indent < 0) {
                indent = doc[1].length;
            } else if (indent != doc[1].length) {
                break;
            }
            if (doc[2].indexOf("<param name=\"me\"") > 0) {
                continue;
            }

            vsdoc += doc[0] + "\n";
        }
        if (vsdoc != "") {
            vsdocPattern.lastIndex = 0;
            var funcHeader = funcSrc.substr(0, funcSrc.search(vsdocPattern));
            funcHeader = funcHeader.replace(/(\(\s*)me(, )?/, "");

            var annotation = "(" + funcHeader + vsdoc + "})";
            //intellisense.annotate(published, eval(annotation));
            return eval(annotation);
        }

        return published;
    })
}

当我键入 x.publicFunction( 时,我得到的结果是显示参数 a,但是 vsdoc 中的文档丢失了。我希望签名帮助显示文档以及参数的类型信息,而不是重复自己将文档从 x.js 复制和调整到 x.intellisense.js,如何?

为了在 DesignTime 的智能感知中隐藏前几个参数,使用 Function.prototype.bind 就足够了。

所以我的解决方案是使用以下代码更新 lib.intellisense.js

$.publish = function (fs) {
    /// <summary>Publish a list of functions.</summary>
    /// <param name="fs" type="Object">List of functions to publish</param>

    return $.map(fs, function () {
            var published = this.bind(null, null);
            intellisense.redirectDefinition(published, this);
            return published;
    })
}

现在无需关心如何注释已发布的函数,M$ 的家伙已经为 Function.prototype.bind 做了。