Search code examples
javascriptwebstorm

WebStorm not recognizing dynamic methods


I'm currently writing an app in Object Oriented JavaScript, and I have a method which adds various functions on runtime to a function's prototype chain. The problem with this, is that when I try to use them in WebStorm i get an JSUnresolvedFunction error.

I've tried adding JSDoc to my code in the constructor and in the code itself but it still wont recognize the methods. Here is my code:

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

Solution

  • just remove everything except the @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 */
            }
        }
    }