2016-03-14 60 views
2

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

我试着在构造函数和代码本身中添加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 */ 
     } 
    } 
} 

回答

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 */ 
     } 
    } 
}