【发布时间】:2011-08-03 18:06:20
【问题描述】:
在以下示例中 - 鉴于返回值并不重要 - 是否有理由更喜欢其中一种方法?
// Method 1
function (a, b) {
if (a == b){
// I'm just interested in
// the stuff happening here
} else {
// or here
}
return true;
}
// Method 2
function (a, b) {
if (a == b){
// I'm just interested in
// the stuff happening here
return true;
}
// or here
return true;
}
【问题讨论】:
-
您可能希望使用
==而不是=,因为您是在比较而不是设置值。 ;) 一个简单的return a == b;有时也可以解决问题。 -
只是出于好奇,为什么要写一个总是返回true或者返回值没有意义的方法呢?没有边缘情况吗?
-
如果返回值不重要,那为什么还要显式返回呢?让代码“通过”。
-
a === b优于a == b以防止类型强制 -
我只是想明确一点,我真的不在乎结果,就好像它总是一样的。
标签: javascript