Reading underscore's source, I noticed the use of void 0
instead of undefined
. I know in some browsers undefined can be overwritten, and that a solution to this, in many cases, is just omitting an argument when calling a function, or return;
-ing. In fact, for minification purposes, it makes much more sense to do this rather than using void 0
.
在阅读下划线的源代码时,我注意到void 0而不是undefined。我知道在某些浏览器中没有定义可以被覆盖,而且在很多情况下,解决这个问题的方法就是在调用函数或返回时省略一个参数。事实上,为了缩小规模,这样做更有意义,而不是使用void 0。
Also, jquery's aproach to this issue:
此外,jquery对这个问题的建议是:
(function (window, undefined){
/* ... */
}(window));
seems to be better in every sense. It is much more readable than void 0
, can be minified further, and might give some tiny performance boost as explained in the linked answer.
似乎在各个方面都更好。它比void 0可读性强得多,可以进一步缩小,并且可以提供一些小的性能提升,如链接答案中所解释的那样。
OK, void 0 appears about 6 times in underscore and 9 in backbone, so it doesn't make much of a difference. So, my question is: Is there any reason or corner cases where void 0
is preferable?
void 0在下划线中出现了大约6次,在主干中出现了9次,所以没有多大区别。所以,我的问题是:是否有任何理由或角落的情况下,void 0是更可取的?
2 个解决方案
#1
1
Here's an example of why the "undefined argument" thing can be a horrible idea.
这里有一个例子,解释为什么“未定义的论点”是一个可怕的想法。
Let's say you get used to doing it. And you start applying it to other functions too, like this:
假设你习惯了。你也可以把它应用到其他函数中,比如:
function doSomething(undefined) {
// blah blah blah
if( something == undefined) {
// problem
}
}
All good, right?
一切美好的,对吧?
Let's say that this function is an event handler.
假设这个函数是一个事件处理程序。
someElement.onclick = doSomething;
Oh dear. doSomething
gets passed an Event object, which is most certainly not undefined
!
哦亲爱的。doSomething获得一个事件对象,这个对象肯定不是没有定义的!
void 0
is much more robust, as it doesn't rely on a quirk or an assumption to work.
void 0要健壮得多,因为它不依赖于一个怪癖或假设来工作。
#2
0
As you said, void 0
means undefined
. Since older browsers undefined
can be overwritten. void 0
is guaranteed and will always be undefined. But i do not think, there is no much difference between no argument undefined(jquery way) and void 0
.
正如你所说,void 0表示没有定义。由于未定义的旧浏览器可以被覆盖。void 0是有保证的,并且总是没有定义的。但是我认为,在没有未定义的参数(jquery方式)和void 0之间没有什么区别。
I think, void 0
is just a coding preference.
我认为,void 0只是一个编码偏好。
#1
1
Here's an example of why the "undefined argument" thing can be a horrible idea.
这里有一个例子,解释为什么“未定义的论点”是一个可怕的想法。
Let's say you get used to doing it. And you start applying it to other functions too, like this:
假设你习惯了。你也可以把它应用到其他函数中,比如:
function doSomething(undefined) {
// blah blah blah
if( something == undefined) {
// problem
}
}
All good, right?
一切美好的,对吧?
Let's say that this function is an event handler.
假设这个函数是一个事件处理程序。
someElement.onclick = doSomething;
Oh dear. doSomething
gets passed an Event object, which is most certainly not undefined
!
哦亲爱的。doSomething获得一个事件对象,这个对象肯定不是没有定义的!
void 0
is much more robust, as it doesn't rely on a quirk or an assumption to work.
void 0要健壮得多,因为它不依赖于一个怪癖或假设来工作。
#2
0
As you said, void 0
means undefined
. Since older browsers undefined
can be overwritten. void 0
is guaranteed and will always be undefined. But i do not think, there is no much difference between no argument undefined(jquery way) and void 0
.
正如你所说,void 0表示没有定义。由于未定义的旧浏览器可以被覆盖。void 0是有保证的,并且总是没有定义的。但是我认为,在没有未定义的参数(jquery方式)和void 0之间没有什么区别。
I think, void 0
is just a coding preference.
我认为,void 0只是一个编码偏好。