JSLint错误:将调用移到包含该函数的括号中 [英] JSLint error: Move the invocation into the parens that contain the function

查看:262
本文介绍了JSLint错误:将调用移到包含该函数的括号中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使此代码jsLint Happy?

How can i make this code jsLint Happy ?

;(function ( $, window, document, undefined ) {
  //some code 
})( jQuery, window, document );

建议我这样写吗?有什么区别吗?我该怎么办?

It advises me to write it this way ? Will there be any difference ? What do i do ?

(function ( $, window, document, undefined ) {
  //some code 
}( jQuery, window, document ));

推荐答案

我要从JSLint堆栈中的下一个问题中复制我的答案.这就是克罗克福德说第二种方法更好的原因.

I'm copying my answer from the next question in the JSLint stack. Here's why Crockford says that the second way is better.

来自 http://javascript.crockford.com/code.html

当要立即调用一个函数时,整个调用 表达式应该用括号括起来,这样很明显 产生的价值是功能的结果,而不是 功能本身.

When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself.

稍微扩展一下,众所周知,JavaScript并不是强类型语言.将function推送到变量中并不少见,实际上,将功能推送到var中比直接调用匿名函数中的值更常见.

To expand a little, JavaScript isn't, as we know, a strongly typed language. It's not uncommon to push a function into a variable, and, in fact, it could be more common to push a function into a var than a value from an immediately invoked anonymous function.

因此值得在代码中显示一些语法糖,以显示何时调用而不是设置对函数的引用.

So it's worth having a little syntactic sugar in your code to show when you're invoking rather than setting a reference to a function.

不要被那些抱怨无法分开 Douglas Crockford 的人所困扰. JSLint的意见.他们是对的,那就太好了!

Don't get bogged down by those that complain that it's impossible to separate Douglas Crockford's opinions from JSLint. They're right, and that's good!

在一个足够大的项目中,为什么您会不同意他?我从未见过Crockford要求客观上是错的. JSLint中的所有规则都可以说是很好的规则,共享项目中的代码标准化通常是一件很不错的事情.他正在免费教您好(!坏?)习惯.值得一起玩.

In a big enough project, why would you have to disagree with him? I'm never seen Crockford require something that's objectively wrong. All of the rules in JSLint are arguably good ones, and standardization of code in shared projects is often A Very Good Thing. He's teaching you good (!bad?) habits for free. It's worth playing along.

(领先的分号,我知道你要去哪里了-防守很好.但是,如果每个人都使用JSLint,我仍然会在那儿感到疼痛.)

(The leading semi-colon, I see where you're going -- defensive is good. But if everyone used JSLint... I still feel your pain there a little.)

这篇关于JSLint错误:将调用移到包含该函数的括号中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆