问题描述
我有这段代码(摘自这个问题):
I have this piece of code (taken from this question):
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err)
return done(err);
var pending = list.length;
if (!pending)
return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending)
done(null, results);
});
} else {
results.push(file);
if (!--pending)
done(null, results);
}
});
});
});
};
我正在尝试遵循它,我想我理解所有内容,除了结尾处显示!-pending
的内容.在这种情况下,该命令有什么作用?
I'm trying to follow it, and I think I understand everything except for near the end where it says !--pending
. In this context, what does that command do?
感谢所有其他评论,但问题已得到多次解答.还是谢谢你!
I appreciate all the further comments, but the question has been answered many times. Thanks anyway!
推荐答案
!
会反转一个值,并为您提供相反的布尔值:
!
inverts a value, and gives you the opposite boolean:
!true == false
!false == true
!1 == false
!0 == true
-[value]
从一个数字中减去一(1),然后返回该数字以供使用:
--[value]
subtracts one (1) from a number, and then returns that number to be worked with:
var a = 1, b = 2;
--a == 0
--b == 1
因此,!-pending
从未决中减去1,然后返回其真实/虚假值的相反值(是否为 0
).
So, !--pending
subtracts one from pending, and then returns the opposite of its truthy/falsy value (whether or not it's 0
).
pending = 2; !--pending == false
pending = 1; !--pending == true
pending = 0; !--pending == false
是的,请遵循ProTip.在其他编程语言中,这可能是常见的习惯用法,但是对于大多数声明性JavaScript编程而言,这似乎很陌生.
And yes, follow the ProTip. This may be a common idiom in other programming languages, but for most declarative JavaScript programming this looks quite alien.
这篇关于什么是“!-"?用JavaScript做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!