从将传递 jslint 的同一对象中的函数调用另一个函数的正确方法是什么?

问题描述

从将传递 jslint 的同一对象中的函数调用一个函数的正确方法是什么?

任何信息将不胜感激!

用于说明目的的简单示例:

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

解决方法

添加指令 /*jslint devel,this*/ 应该可以修复它。选项/指令的完整列表可在@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();
}());
,

专业提示:在修复代码时不要使用任何必要的 jslint 指令,以最大限度地发挥 JSLint 的优势。

JSLint 希望你这样安排你的代码(不需要 this。尽可能避免 this):

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...