javascript - WebStorm 无法识别动态方法

标签 javascript webstorm

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

相关文章:

javascript - 编写 Node.js 代码时,app.send 不是 WebStorm 中的函数错误

javascript - 如何在 angularjs tabset 中呈现 html 内容?

javascript - 如何在 erb 中安全地呈现 URI

intellij-idea - PyCharm 中的 ___jb_bak___ 和 ___jb_old___ 文件

javascript - JetBrains WebStorm 中的 native 或底层 JavaScript 文件

javascript - 在 WebStorm IDE 中突出显示自定义语法结构

javascript - 设置 ContentPane 内容时如何保留旧内容

javascript - 同一事件和节点上的事件处理程序以相反的顺序运行

javascript - meteor .js : lib folder not loaded first

intellij-idea - 在 webstorm/intellij 上创建 javascript 函数的快捷方式