WebStorm无法识别动态方法

3

我目前正在使用面向对象的JavaScript编写应用程序,并且我有一个方法,该方法在运行时将各种函数添加到函数的原型链中。问题是,当我尝试在WebStorm中使用它们时,我会得到一个JSUnresolvedFunction错误。

我尝试在构造函数和代码本身中添加JSDoc,但仍然无法识别这些方法。以下是我的代码:

/**
 * Example class
 * @constructor
 *
 * @member {Function} OnConnect        <-- Doesn't work
 * @var {Function} OnConnect           <-- Doesn't work either
 * @typedef {Function} OnConnect       <-- You get the deal
 * @property {Function} OnConnect      <-- Same for this
 */
function MyClass() 
{
    // Add methods dynamically
    this.addMethods(["OnConnect", "OnDisconnect"]);

    // Add callback listener to 'OnConnect'
    // This is where WebStorm doesn't recognize my methods
    this.OnConnect(function() { 
        console.log('Callback fired!'); 
    });
}

/**
 * Add methods which do the same thing to the class
 * @param {Array} methods
 * @returns {void}
 */
MyClass.prototype.addMethods = function(methods) 
{
    for (var i in methods) {
        this[methods[i]] = function(callback) {
            /** Tons of re-used logic here */
        }
    }
}
1个回答

3

只需保留 @property ,删除其他内容即可。

    /**
     * Example class
     * @constructor
     *
     * @property {Function} OnConnect
     */

    function MyClass() 

{
    // Add methods dynamically
    this.addMethods(["OnConnect", "OnDisconnect"]);

    // Add callback listener to 'OnConnect'
    // This is where WebStorm doesn't recognize my methods
    this.OnConnect(function() { 
        console.log('Callback fired!'); 
    });
}

/**
 * Add methods which do the same thing to the class
 * @param {Array} methods
 * @returns {void}
 */
MyClass.prototype.addMethods = function(methods) 
{
    for (var i in methods) {
        this[methods[i]] = function(callback) {
            /** Tons of re-used logic here */
        }
    }
}

如果该方法接受参数,是否可以在自动完成中显示参数? - vir us

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接