node官方文档给的说明是
The asynchronous form always take a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.
在低版本浏览器中undefined值可能被修改,在node中似乎没有这个困扰。
从语义上来说应该传null,但是如果按照这个标准,没有错误也没有返回值的时候就必须要调callback(null)
而不能直接调callback()
了么?
两个混着用会造成多严重的后果?
先放结论:callback()
和callback(null)
是一样的。
首先callback(null)
无疑是正确的用法,null
表示没有错误。
其次,在函数中传undefined或者干脆什么都不传,就像callback()
一样。表示尊重这个参数的默认值。
恰巧,callback
函数的第一个参数的默认值就是null
。
所以callback()
和callback(null)
是一样的。
迷茫2017-04-17 11:06:18
After writing it, I realized that most of the following content is off-topic "reference material" and is something that should be blogged.
Looking at this example alone, it seems that neither null
nor undefined
happens to report an error. However, from the perspective of JS programming habits, you must pay attention to:
undefined
Basically it should not be used directly as a valid "value" to assign to a variable, although JS allows this. undefined
and simply leaving it blank have the same semantics in syntax, but in terms of look and feel, it is even easy to create semantic ambiguity. (That is, it seems that you are passing in the undefined
value, but for the inside of the function, it is the same as not writing it. In order to implement the default parameters, the function may change the parameters of undefined
to other values) undefined
is a waste of effort, so it’s better not to write it at all. null
is definitely the best way to avoid ambiguity. If you are interested in the following sideshows, please continue watching.
Too long! Throw in http://blog.segmentfault.com/shamiao/1190000000381846
阿神2017-04-17 11:06:18
Node.js is a server-side technology and does not have the problem of low-version browsers. Most people use callback(null)
高洛峰2017-04-17 11:06:18
I think using null is more semantic. Null means that the content of the variable is empty and has no content, while undefined means that the variable is undefined.
PHP中文网2017-04-17 11:06:18
I just discussed it in Brother Miao’s blog, let’s sort it out here.
There is no reserved word undefined
in JavaScript. So err == undefined
is only used to determine whether the values of variable err
and variable undefined
are both "unassigned" - obviously the latter is unassigned.
In fact, the description in the official document may be an ambiguity. What it wants to express should be "the first parameter should be null
or unassigned", an adjective.
Therefore, when callback()
the first parameter is in an unassigned state, even if you assign a value to the variable undefined
, this situation will not change.
Then callback()
and callback(null)
are the same.
function (err, a, b, c) {
}
In most cases, you can just treat err
as a Boolean.
If you really wonder whether it is undefined, then please write this:
if (typeof err == 'undefined') {
// ..
}
PS: Why is SF’s Markdown parsing so weird today. . .
伊谢尔伦2017-04-17 11:06:18
Let’s take a look at the execution of callback. In the definition of callback, it is usually written like this:
function callback(err) {
console.log(err === undefined);
}
From this point of view, when executing callback, callback()
and callback(undefined)
are the same. But we usually judge the first parameter in this way if(!err)
. In this case, if it is not passed, undefined or null can be passed, but null is more semantic and corresponds to the Error object, clearly stating "execute the callback without errors".