Javascript 难度-传递给内部函数的参数和括号的使用
Javascript difficulty - parameter passed to inner function and the use of parenthesis
有人可以揭开以下神秘面纱吗?
const connection = (closure) => {
return mongoClient.connect(connectionString,(err,db) => {
if(err) return console.log(err);
closure(db);
});
};
显然:
- 'connection' 常量正在存储一个接受参数的函数(闭包)
- mongoClient.connect() returns null 但反过来填充回调中的参数。
- 如果发生错误,错误将记录到控制台,但尽管如此(这是最令人困惑的部分)闭包使用括号将自身包裹在 db 周围,这给出了?
- 因为 connect() return 为 null,是否可以将其替换为某些赋值语句(例如 closure = db;),然后删除 return 语句?这里需要一个 return 语句是什么?
谢谢你所做的一切
阿尔
If an error occurs err logs to the console but nonetheless (and here is the most confusing part) closure makes use of parenthesis to wrap itself around db which gives?
不是"nonetheless"。 closure
行仅在没有错误时发生,因为如果有错误,函数会立即 returned。
此外,closure
后面的括号只是调用它,所以closure
应该是一个函数。不管它 returns 将是它的结果。
Since connect() returns null, could this have been replaced with some assignment statement (such as closure = db;) and then the return statement removed? What is the need of a return statement here?
return
只是退出功能。 returning console.log
的结果这一事实在这里意义不大。他们这样做只是为了将其放入单个语句中,这样他们就不需要花括号了。
const connection = (closure) => {
return mongoClient.connect(connectionString,(err,db) => {
if(err) {
console.log(err);
return;
}
closure(db);
});
};
我假设你指的是最里面的 return
。外部的不能使用 db
因为它只存在于传递给 connect
的回调中。如果 .connect()
总是 returns null
,那么是的,不需要那个外部 return
,但是同样,你不能只使用 db
。其余代码仍然是必要的。
明确地说,您无法从传递给 connect
的回调中取回任何值。它在你的外部函数和 .connect()
return.
之后很久被调用
有人可以揭开以下神秘面纱吗?
const connection = (closure) => {
return mongoClient.connect(connectionString,(err,db) => {
if(err) return console.log(err);
closure(db);
});
};
显然:
- 'connection' 常量正在存储一个接受参数的函数(闭包)
- mongoClient.connect() returns null 但反过来填充回调中的参数。
- 如果发生错误,错误将记录到控制台,但尽管如此(这是最令人困惑的部分)闭包使用括号将自身包裹在 db 周围,这给出了?
- 因为 connect() return 为 null,是否可以将其替换为某些赋值语句(例如 closure = db;),然后删除 return 语句?这里需要一个 return 语句是什么?
谢谢你所做的一切 阿尔
If an error occurs err logs to the console but nonetheless (and here is the most confusing part) closure makes use of parenthesis to wrap itself around db which gives?
不是"nonetheless"。 closure
行仅在没有错误时发生,因为如果有错误,函数会立即 returned。
此外,closure
后面的括号只是调用它,所以closure
应该是一个函数。不管它 returns 将是它的结果。
Since connect() returns null, could this have been replaced with some assignment statement (such as closure = db;) and then the return statement removed? What is the need of a return statement here?
return
只是退出功能。 returning console.log
的结果这一事实在这里意义不大。他们这样做只是为了将其放入单个语句中,这样他们就不需要花括号了。
const connection = (closure) => {
return mongoClient.connect(connectionString,(err,db) => {
if(err) {
console.log(err);
return;
}
closure(db);
});
};
我假设你指的是最里面的 return
。外部的不能使用 db
因为它只存在于传递给 connect
的回调中。如果 .connect()
总是 returns null
,那么是的,不需要那个外部 return
,但是同样,你不能只使用 db
。其余代码仍然是必要的。
明确地说,您无法从传递给 connect
的回调中取回任何值。它在你的外部函数和 .connect()
return.