Javascript Strict Violation 使用此关键字并揭示模块模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6300937/
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
Strict Violation using this keyword and revealing module pattern
提问by Matty F
Having trouble getting the following to pass jslint/jshint
无法通过 jslint/jshint 获取以下内容
/*jshint strict: true */
var myModule = (function() {
"use strict";
var privVar = true,
pubVar = false;
function privFn() {
return this.test; // -> Strict violation.
}
function pubFn() {
this.test = 'public'; // -> Strict violation.
privFn.call(this); // -> Strict violation.
}
return {
pubVar: pubVar,
pubFn: pubFn
};
}());
myModule.pubFn();
I understand it's being caused by the use of this
in a function declaration, but I read something Crockford wrote and he said the violation is meant to prevent global variable pollution - but the only global variable here is the one I'm explicitly defining... myModule
. Everything else is held in the immediate function scope, and I should be able to use this
to refer to the module.
我知道它是由this
在函数声明中使用引起的,但是我读了 Crockford 写的一些东西,他说违规是为了防止全局变量污染 - 但这里唯一的全局变量是我明确定义的那个...... myModule
. 其他所有内容都保存在直接函数作用域中,我应该可以使用它this
来引用模块。
Any ideas how I can get this pattern to pass?
任何想法如何让这个模式通过?
Update:if I use a function expression instead of a declaration, this seems to work, ie
更新:如果我使用函数表达式而不是声明,这似乎有效,即
var pubFn = function () { ...
I'm not a fan of this format though, prefer to have the function name and named params closer and the declaration looks/feels cleaner. I honestly don't see why this is throwing the violation - there's no reason for it in this pattern.
不过,我不喜欢这种格式,更喜欢让函数名和命名参数更接近,并且声明看起来/感觉更干净。老实说,我不明白为什么这会引发违规 - 在这种模式中没有理由。
回答by markrian
JSHint has an optioncalled validthis
, which:
JSHint 有一个名为的选项validthis
,它:
[...] suppresses warnings about possible strict violations when the code is running in strict mode and you use
this
in a non-constructor function [...], when you are positive that your use ofthis
is valid in strict mode.
[...] 当代码在严格模式下运行并且您
this
在非构造函数中使用时,抑制有关可能的严格违规的警告[...],当您确定您的使用this
在严格模式下有效时。
Use it in the function that JSHint is complaining about, which in your case, would look like this:
在 JSHint 抱怨的函数中使用它,在你的情况下,它看起来像这样:
function privFn() {
/*jshint validthis: true */
return this.test; // -> No Strict violation!
}
function pubFn() {
/*jshint validthis: true */
this.test = 'public'; // -> No Strict violation!
privFn.call(this); // -> No Strict violation!
}
It might seem like a pain to have to specify that in each function where it applies, but if you set the option at the top of your module function, you may hide genuinestrict mode violations from yourself.
必须在每个应用它的函数中指定它似乎很痛苦,但是如果您在模块函数的顶部设置该选项,您可能会对自己隐藏真正的严格模式违规。
回答by Lars Gyrup Brink Nielsen
The real problem here is that if you call privFn
from within the module context(from within the IIFE), this
will be undefined
when in strict mode; window
if not in strict mode. Alas, the function would fail if called from within the IIFE.
这里真正的问题是,如果您privFn
从模块上下文中(从IIFE 中)调用,this
将是undefined
在严格模式下;window
如果不是严格模式。唉,如果从 IIFE 内部调用该函数将失败。
This is because the functions have no owner(object) when called from within an IIFE, whereas the returned module objectis the owner of the functions when they are called from outside the IIFE context, e.g. this === myModule
when calling myModule.pubFn()
.
这是因为当从 IIFE 内部调用时,函数没有所有者(对象),而从 IIFE 上下文外部调用时,返回的模块对象是函数的所有者,例如this === myModule
调用myModule.pubFn()
.
Both strict mode and JSHint/JSLint are trying to help you and you should never just ignore the errors/warnings generated by them, but instead figure out why they are warning you.
严格模式和 JSHint/JSLint 都试图帮助你,你永远不应该忽略它们产生的错误/警告,而是弄清楚它们为什么警告你。
If you are 100 percent sure that privFn
, pubFn
, etc. will not be called anywhere but outside your module, just put in the comment /*jshint validthis: true */
in any functions that generate a warning. Alternatively, one comment in the IIFE will prevent JSHint from generating this error on any function inside the module.
如果你是100%的肯定privFn
,pubFn
等不会被任何地方,但你的模块外调用,只是把在评论/*jshint validthis: true */
中生成一个警告任何功能。或者,IIFE 中的一个注释将阻止 JSHint 在模块内的任何函数上生成此错误。
One of the many possible solution
许多可能的解决方案之一
Store the scope of this
(in self
in this example) to refer explicitly to the module. This will show and ensure your intent.
存储this
(在self
本例中)的范围以明确引用模块。这将显示并确保您的意图。
/*jshint strict: true */
var myModule = (function() {
"use strict";
var privVar = true,
pubVar = false,
self = this;
function privFn() {
return self.test;
}
function pubFn() {
self.test = 'public';
//privFn.call(this); // Will have no effect, as `privFn` does not reference `this`
privFn();
}
return {
pubVar: pubVar,
pubFn: pubFn
};
}());
myModule.pubFn();
回答by Matty F
Unfortunately, this is the intended error for this setup since jslint/jshint don't know the function declared in global context is to be later used as an object method.
不幸的是,这是此设置的预期错误,因为 jslint/jshint 不知道在全局上下文中声明的函数稍后将用作对象方法。