Skip to content

Instantly share code, notes, and snippets.

@JacksonTian
Last active December 13, 2015 22:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacksonTian/4983675 to your computer and use it in GitHub Desktop.
Save JacksonTian/4983675 to your computer and use it in GitHub Desktop.
Node代码中最常见的几种废代码

三元表达式

var foo = bar === 'xxx' ? true : false;
// 更好的代码是:
var foo = bar === 'xxx';

两个和三个等号的判断,都是得到布尔值。

回调函数的传递

Topic.count(query,function(err,count) {
  if(err) return cb(err);
  return cb(err,count);
});

// 更好的代码是:
Topic.count(query, cb);

假值赋值

if (!foo) foo = 'bar';
// 如下更好
foo = foo || 'bar';

if判断

if (flag == true) {
  // then...
}
// better:
if (flag) {
  // then...
}
@ibigbug
Copy link

ibigbug commented Feb 19, 2013

第一种很容易理解。请问第二种该如何理解?

@garrydzeng
Copy link

因为回调写多余了.

@maisui99
Copy link

把间接调用改成直接调用呗。。。

@lzosi
Copy link

lzosi commented Feb 19, 2013

插入。。

@chx007
Copy link

chx007 commented Feb 19, 2013

还有

if (!foo) foo = 'bar' 

不如

foo = foo || 'bar' 

@winglechen
Copy link

标题党啊

@rhyzx
Copy link

rhyzx commented Feb 24, 2013

if (flag === true) {
    // then...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment