Javascript JSHint “可能严格违规。” 使用`bind`时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12057427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSHint "Possible strict violation." when using `bind`
提问by Florian Margaine
Consider this simple code:
考虑这个简单的代码:
"use strict";
var obj = {
f: function() {
this.prop = 'value';
g.bind( this )();
}
};
function g() {
console.log( this.prop );
}
If I try to validate this code, jshint gives me the error Possible strict violation.
where I call console.log( this.prop );
. This is because this
is undefined in strict mode in a function.
如果我尝试验证此代码,jshint 会Possible strict violation.
在我调用console.log( this.prop );
. 这是因为this
在函数的严格模式下是 undefined 。
But I'm binding this function before calling it, so this
is the correct object.
但是我在调用它之前绑定了这个函数,所以this
是正确的对象。
I'm using this "design pattern" to avoid cluttering the main object. Passing the properties in the parameters will also clutter the function, so I refuse to do this. Besides, this is exactly what bind
is for.
我正在使用这种“设计模式”来避免弄乱主要对象。在参数中传递属性也会使函数混乱,所以我拒绝这样做。此外,这正是bind
它的目的。
Is there a way for JSHint to let me do this?
JSHint 有没有办法让我这样做?
回答by Anton Kovalyov
It is extremely hard to detect this case without running the code. You can use option validthis
to suppress this warning:
如果不运行代码,很难检测到这种情况。您可以使用选项validthis
来抑制此警告:
"use strict";
var obj = {
f: function() {
this.prop = 'value';
g.bind( this )();
}
};
function g() {
/*jshint validthis:true */
console.log( this.prop );
}
It is to be noted that jshint comments are function scoped. So the comment will work for the function g
and its inner functions, not just the next line.
需要注意的是,jshint 注释是函数作用域的。所以注释将适用于函数g
及其内部函数,而不仅仅是下一行。
回答by George
You can also achieve the same effect if you modify your code to the following to avoid using this
all together.
如果将代码修改为以下以避免this
一起使用,也可以达到相同的效果。
"use strict";
var obj = {
f: function() {
this.prop = 'value';
g.bind( null, this )();
}
};
function g(self) {
console.log( self.prop );
}
回答by xgrtl
Here's a simpler solution that doesn't require any change of pattern or specific markup for jshint:
这是一个更简单的解决方案,不需要对 jshint 的模式或特定标记进行任何更改:
"use strict";
var obj = {
f: function() {
this.prop = 'value';
G.bind( this )();
}
};
function G() {
console.log( this.prop );
}
jshint assumes that you're following the convention that functions starting with an uppercase letter are classes which will be instantiated and always having this
available.
jshint 假设您遵循惯例,即以大写字母开头的函数是将被实例化并始终this
可用的类。
回答by Micha? Wojas
Try:
尝试:
"use strict";
var obj = {
f: function() {
this.prop = 'value';
g.bind( this )();
}
};
var g = function() {
console.log( this.prop );
}
回答by George
This is a different "design pattern" as you put it, it achieves the same thing, but avoids the problem entirely.
正如您所说,这是一种不同的“设计模式”,它实现了相同的目的,但完全避免了问题。
"use strict";
function obj() {
this.prop = '';
}
obj.prototype.f = function obj_f() {
this.prop = 'value';
this.g();
};
obj.prototype.g = function obj_g() {
console.log( this.prop );
};
you would invoke it like thus:
你会像这样调用它:
var myO = new obj();
myO.f();