WebStorm无法识别动态方法

时间:2016-03-14 09:33:07

标签: javascript webstorm

我目前正在使用面向对象的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 个答案:

答案 0 :(得分:2)

只删除@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 */
        }
    }
}