简体   繁体   中英

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

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(); }());

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();
}());

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();
}());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM