0

What is the proper way to call another function from a function within the same object that will pass jslint?

Any information would be greatly appreciated!

Simple example for illustration purposes:

(function () {
    "use strict";

    var myObject = {
        functionOne: function () {
            console.log("functionOne called by functionTwo");
        },
        functionTwo: function () {
            // jslint Error: 'myObject' is out of scope. (out_of_scope_a);
            // What is the proper way to call another function inside the same Object that will pass jslint?
            myObject.functionOne();

            // jslint Error: Unexpected 'this'. (unexpected_a);
            // What is the proper way to call another function inside the same Object that will pass jslint?
            this.functionOne();
        }
    };

    myObject.functionTwo();
}());
  • 4
    The only error I see from your code is `Unexpected 'this'.`. The `myObject.functionOne();` does not result in the error you describe – CertainPerformance Apr 15 '21 at 02:26
  • 1
    Some good advice in [this post](https://stackoverflow.com/questions/30314944/jslint-error-unexpected-this). I'd pay particularly close attention to _"tell JSLint to shut up... Or don't use JSLint at all"_ – Phil Apr 15 '21 at 02:29
  • Yeah, I'd highly recommend using something more configurable, less opinionated, and more modern, like ESLint. – CertainPerformance Apr 15 '21 at 02:30
  • Thank you for the replies. @CertainPerformance you put me on the right track. I had an outdated jslint version. The latest version doesn't mind myObject.functionOne(); – Project Arcade Apr 15 '21 at 04:51

2 Answers2

0

adding directives /*jslint devel, this*/ should fix it. the full list of options/diretives is available @ https://www.jslint.com/

/*jslint devel, this*/
(function () {
    "use strict";

    var myObject = {
        functionOne: function () {
            console.log("functionOne called by functionTwo");
        },
        functionTwo: function () {
            // jslint Error: 'myObject' is out of scope. (out_of_scope_a);
            // What is the proper way to call another function inside the
            // same Object that will pass jslint?
            myObject.functionOne();

            // jslint Error: Unexpected 'this'. (unexpected_a);
            // What is the proper way to call another function inside the same
            // Object that will pass jslint?
            this.functionOne();
        }
    };

    myObject.functionTwo();
}());
kai zhu
  • 149
  • 1
  • 7
  • There's no need to use the `this` directive. You're well familiar with Crockford's critique that using `this` makes JavaScript code like an Abbott and Costello skit! ;^D Refactor as in my answer and you can skip that shortcut -- and have more manageable code! – ruffin Jul 21 '21 at 18:28
0

Pro tip: Refrain from using any but necessary jslint directives when fixing code to maximize the benefits of JSLint.

JSLint would prefer you arrange your code like this (no this required. Avoid this where possible):

/*jslint devel */
(function () {
    "use strict";
    var myObject;

    function functionOneDef() {
        console.log("functionOne called by functionTwo");
    }

    function functionTwoDef() {
        functionOneDef();
    }

    myObject = {
        functionOne: functionOneDef,    
        functionTwo: functionTwoDef
    };

    myObject.functionTwo();
}());
ruffin
  • 16,507
  • 9
  • 88
  • 138