简体   繁体   English

JSHint “可能严格违规。” 使用`bind`时

[英]JSHint "Possible strict violation." when using `bind`

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.如果我尝试验证此代码,jshint 会给我错误Possible strict violation. where I call console.log( this.prop );我在那里调用console.log( this.prop ); . . This is because this is undefined in strict mode in a function.这是因为在函数的严格模式下this是未定义的。

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 有没有办法让我这样做?

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.需要注意的是,jshint 注释是函数作用域的。 So the comment will work for the function g and its inner functions, not just the next line.所以注释将适用于函数g及其内部函数,而不仅仅是下一行。

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 );
}

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可用。

Try:尝试:

"use strict";

var obj = {
    f: function() {
        this.prop = 'value';
        g.bind( this )();
    }
};

var g = function() {
    console.log( this.prop );
}

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM