Page 178 - Node.js开发指南
P. 178

172  附录 B   Node.js 编程规范


             与你的预期不同,例如下面错误的例子,num == literal的值是true。
                 正确的等号用法:

                 var num = 9;
                 var literal = '9';
                 if (num === literal) {
                   console.log('Should not be here!!!');
                 }
                 错误的等号用法:

                 var num = 9;
                 var literal = '9';
                 if (num == literal) {
                   console.log('Should not be here!!!');
                 }


             B.10  命名函数


                 尽量给构造函数和回调函数命名,这样当你在调试的时候可以看见更清晰的调用栈。
                 对于回调函数,Node.js的API和各个第三方的模块通常约定回调函数的第一个参数是错
             误对象err,如果没有错误发生,其值为 undefined。
                 正确:

                 req.on('end', function onEnd(err, message) {
                   if (err) {
                     console.log('Error.');
                   }
                 });

                 function FooObj() {
                   this.foo = 'bar';
                 }
                 错误:

                 req.on('end', function (message, err) {
                   if (err === false) {
                     console.log('Error.');
                   }
                 });

                 var FooObj = function() {
                   this.foo = 'bar';
                 }
   173   174   175   176   177   178   179   180   181   182   183