我目前正在使用面向对象的 JavaScript 编写一个应用程序,并且我有一个方法可以在运行时将各种函数添加到函数的原型(prototype)链中。这个问题是,当我尝试在 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 */
}
}
}
最佳答案
只需删除除@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 */
}
}
}
关于javascript - WebStorm 无法识别动态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983848/