微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从将传递 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();
}());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。