在本文档中...
http://nodejs.org/api/domain.html
...这一行出现:
var PORT = +process.env.PORT || 1337;
加号是拼写错误吗?如果不是,说明什么?
最佳答案
加号是一元运算符,它将 process.env.PORT
强制转换为字符串中的数字。
背景:
// since all env variables are strings
process.env.PORT = 'somePortSavedAsSTring';
process.env.PORT
必须是字符串,如果什么都不做, Node 将抛出错误。使用 +
符号可以防止这种情况发生,方法是将字符串(将其从字符串更改为数字)添加为空。
port = ( nothing ) + 'somePortSavedAsSTring'; // makes it a number!
// whitespace is removed by convention, so other programmers know the intent
port = +'somePortSavedAsSTring';
这种方式使用加号只是一种确保变量类型的 Eloquent 方式。你可以使用:
var PORT = Number(process.env.PORT) || 1337;
并获得完全相同的效果。归根结底,这完全取决于您的编码风格。
关于node.js - +process 中的加号是 Node.js 域文档中的拼写错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751705/