javascript jshint 期望函数的新“前缀”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10668111/
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 expects the new 'prefix' for functions
提问by CS_2013
CSiginIn
, CSignUp
, CTryIt
, CBlocks
are all functions declared as such
CSiginIn
, CSignUp
, CTryIt
,CBlocks
都是这样声明的函数
function CSignIn(){//stuff here}
yet JSHint says that I am missing the 'new' 'prefix'. What can I do to fix this?
然而 JSHint 说我缺少“新的”“前缀”。我能做些什么来解决这个问题?
They are just functions inside the module pattern. Also, it is asking me to remove semicolons I had placed at the end of the function which I have done.
它们只是模块模式中的函数。此外,它要求我删除我在已完成的函数末尾放置的分号。
var Control = ( function ()
{
/**
*Publik
*/
var publik = function ( page )
{
// page 1 initialization
if( page == 1 )
{
CSignIn();
CSignUp();
CTryIt();
CBlocks();
}
Function Example...
函数示例...
function CTryIt()
{
// pull elements
var tryit_button = document.getElementById( 'tryit_button' );
// initialize access to Model
tryit_button.addEventListener( "click", function( )
{
new AjaxRequest().invoke( 'ajax_type=ControlTryIt',
function( server_response_text )
{
new AjaxResponse( server_response_text, 'page_change' );
} );
}, false );
}
回答by Felix Kling
If newcap
is enabled, JSHint expects functions starting with a capital letter to be constructors and therefore to be called with the new
keyword.
如果newcap
启用,JSHint 期望以大写字母开头的函数作为构造函数,因此使用new
关键字调用。
Solution: Either disable this option or rename your functions.
解决方案:禁用此选项或重命名您的函数。
From the documentation:
从文档:
This option requires you to capitalize names of constructor functions. Capitalizing functions that are intended to be used with
new
operator is just a convention that helps programmers to visually distinguish constructor functions from other types of functions to help spot mistakes when usingthis
.Not doing so won't break your code in any browsers or environments but it will be a bit harder to figure out—by reading the code—if the function was supposed to be used with or without
new
. And this is important because when the function that was intended to be used withnew
is used without it,this
will point to the global object instead of a new object.function MyConstructor() { console.log(this); } new MyConstructor(); // -> [MyConstructor] MyConstructor(); // -> [DOMWindow]
此选项要求您将构造函数的名称大写。打算与
new
operator一起使用的函数大写只是一种约定,它可以帮助程序员在视觉上将构造函数与其他类型的函数区分开来,以帮助在使用this
.不这样做不会在任何浏览器或环境中破坏您的代码,但是通过阅读代码来弄清楚该函数是否应该与
new
. 这很重要,因为当打算使用的函数在new
没有它的情况下使用时,this
将指向全局对象而不是新对象。function MyConstructor() { console.log(this); } new MyConstructor(); // -> [MyConstructor] MyConstructor(); // -> [DOMWindow]
For a more in-depth understanding on how this
works, read Understanding JavaScript Function Invocation and "this"by Yehuda Katz.
要更深入地了解this
工作原理,请阅读理解 JavaScript 函数调用和Yehuda Katz 的“this”。
回答by GOTO 0
Felix Kling already gave you the correct answer. For completeness, I would note that newcap
defaults to true
(The documentation doesn't state it, but you can read it in the source code). This means that deleting the setting newcap: true
in the JSHint options will not disable the warning: instead, you need to explicitly set newcap: false
.
Felix Kling 已经给了你正确的答案。为了完整起见,我会注意到newcap
默认为true
(文档没有说明,但您可以在源代码中阅读它)。这意味着删除newcap: true
JSHint 选项中的设置不会禁用警告:相反,您需要显式设置newcap: false
.