提问人:Project Arcade 提问时间:4/15/2021 更新时间:7/22/2021 访问量:50
从将传递 jslint 的同一对象中的函数调用另一个函数的正确方法是什么?
What is the proper way to call another function from a function within the same object that will pass 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();
}());
答:
0赞
kai zhu
6/22/2021
#1
添加指令应该可以修复它。选项/选项的完整列表可用@ https://www.jslint.com//*jslint devel, this*/
/*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();
}());
评论
0赞
ruffin
7/22/2021
无需使用该指令。你很熟悉 Crockford 的批评,即使用 JavaScript 使代码像 Abbott 和 Costello 的短剧!;^D按照我的回答进行重构,您可以跳过该快捷方式 - 并获得更易于管理的代码!this
this
0赞
ruffin
7/22/2021
#2
专业提示:在修复代码时,不要使用任何必要的 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();
}());
评论
Unexpected 'this'.
myObject.functionOne();