问题描述
for ( element = 0; element < this.tag_array.length; element++ ) {
document.getElementById( this.tag_array[element] ).addEventListener(
"click", function(){ /* constructor function here */ } ); // jshint.com error #1
}
此代码将在jshint.com b.c中导致错误.它不想看到循环中声明的函数.
This code will cause an error in jshint.com b.c. it does not want to see functions declared in a loop.
但是,如果我传入一个简单的函数引用,那么我将无法再次提取jshint来提取出"this".
However, if I pass in a simple function reference then I can not extract out 'this' with out once again breaking jshint.
for ( element = 0; element < this.tag_array.length; element++ ) {
document.getElementById( this.tag_array[element] ).addEventListener(
"click", vFlipBP );
}
现在,当我在vFlipBP中使用"this"时,出现错误b.c.它希望将"this"与构造函数一起使用.
Now I've when I use 'this' in vFlipBP, I get an error b.c. it expects 'this' to be used in conjunction with a constructor function.
function vFlipBP(){ var foo = this ; } // jshint error #2
因此,我发现无法使jsthint.com在此特定功能中感到满意.
Because of this, I find it impossible to make jsthint.com happy in this particular function.
请不要关闭jshint.com选项.
Nevermind turning off the jshint.com options.
是否可以编写通过jshint.com默认设置的代码.
Is it possible to write code that passes jshint.com default settings.
我只是想了解导致这些警告/错误的原因.
I'm just trying to understand the reasoning behind why it makes these warnings/errors.
我不必担心错误(它是可选的,我可以将其关闭)...我只是想知道是否可以通过默认值...是否错过了一个角度
I'm not hung up on worrying about the error ( it is optional I could just turn it off )...I just want to know if it is possible to pass the default...is there an angle I missed.
推荐答案
由于您似乎并不担心制作几个相同的函数,因此JSHint似乎并不介意此事...
Since you don't seem concerned about making several identical functions, it seems that JSHint doesn't mind this...
var element;
for ( element = 0; element < 10; element++ ) {
document.getElementById( this.tag_array[element] ).addEventListener(
"click", make_func() );
}
function make_func() {
"use strict";
return function(){ var foo = this; alert(foo); };
}
如果您不使用函数声明语法,这似乎也可以接受...
It also seems accepting if you don't use the function declaration syntax...
var element;
var the_func = function(){
"use strict";
var foo = this;
alert(foo);
};
for ( element = 0; element < 10; element++ ) {
document.getElementById( this.tag_array[element] ).addEventListener(
"click", the_func );
}
这篇关于Jshint |传递默认设置函数声明和"this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!