问题描述
从将传递 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();
}());