谁能给我解释一下:'create({email: emailArg} = {}) {}'?
Can someone please explain me this: 'create({email: emailArg} = {}) {}'?
我还在学习 JS。做 Apollo GraphQL 教程:https://www.apollographql.com/docs/tutorial/introduction/
这部分我不太明白:findOrCreateUser({ email: emailArg } = {})
完整代码如下:
/**
* User can be called with an argument that includes email, but it doesn't
* have to be. If the user is already on the context, it will use that user
* instead
*/
async findOrCreateUser({ email: emailArg } = {}) {
const email =
this.context && this.context.user ? this.context.user.email : emailArg;
if (!email || !isEmail.validate(email)) return null;
const users = await this.store.users.findOrCreate({ where: { email } });
return users && users[0] ? users[0] : null;
}
async bookTrips({ launchIds }) {
const userId = this.context.user.id;
if (!userId) return;
let results = [];
// for each launch id, try to book the trip and add it to the results array
// if successful
for (const launchId of launchIds) {
const res = await this.bookTrip({ launchId });
if (res) results.push(res);
}
return results;
}
请不吝赐教或link解释一下就可以了。谢谢
findOrCreateUser({ email: emailArg } = {})
是几个原则的结合
1) 对象解构
const { prop } = obj;
相当于:
const prop = obj.prop;
2) 函数参数解构
同样的事情,但对于函数的参数
function fun({ prop }) {
console.log(prop);
}
等同于
function fun(obj) {
console.log(obj.prop);
}
3) 解构时重命名变量
function fun({ prop: newPropName }) {
console.log(newPropName);
}
等同于
function fun(obj) {
const newPropName = obj.prop;
console.log(newPropName);
}
4) 默认函数参数
function fun(arg = 5) {
console.log(arg);
}
fun(10); // prints 10
fun(); // prints 5 since no value was passed to fun
结论:
findOrCreateUser({ email: emailArg } = {}) {
// [...]
}
相当于
findOrCreateUser(args) {
const emailArg = args ? args.email : {};
// [...]
}
换句话说,它将传递给方法的对象的 email
属性 重命名为 emailArg
并使其直接可用。如果没有向函数传递任何内容,它还会将参数初始化为一个空对象。这是为了避免在没有传递任何内容的情况下引发 Cannot read email of undefined
。
如果您需要更多上下文,请参阅以下文档:
我认为:
1) 该函数希望接收一个 JavaScript 对象作为其第一个参数,该对象内部有一个名为 email
的 属性
2) 在函数内部,我们将 email
属性 称为 emailArg
最后,= {}
部分提供了一个空对象作为默认值,有助于避免错误。
可以在此处找到有关函数参数解构的更详细说明:https://davidwalsh.name/destructuring-function-arguments
我还在学习 JS。做 Apollo GraphQL 教程:https://www.apollographql.com/docs/tutorial/introduction/
这部分我不太明白:findOrCreateUser({ email: emailArg } = {})
完整代码如下:
/**
* User can be called with an argument that includes email, but it doesn't
* have to be. If the user is already on the context, it will use that user
* instead
*/
async findOrCreateUser({ email: emailArg } = {}) {
const email =
this.context && this.context.user ? this.context.user.email : emailArg;
if (!email || !isEmail.validate(email)) return null;
const users = await this.store.users.findOrCreate({ where: { email } });
return users && users[0] ? users[0] : null;
}
async bookTrips({ launchIds }) {
const userId = this.context.user.id;
if (!userId) return;
let results = [];
// for each launch id, try to book the trip and add it to the results array
// if successful
for (const launchId of launchIds) {
const res = await this.bookTrip({ launchId });
if (res) results.push(res);
}
return results;
}
请不吝赐教或link解释一下就可以了。谢谢
findOrCreateUser({ email: emailArg } = {})
是几个原则的结合
1) 对象解构
const { prop } = obj;
相当于:
const prop = obj.prop;
2) 函数参数解构
同样的事情,但对于函数的参数
function fun({ prop }) {
console.log(prop);
}
等同于
function fun(obj) {
console.log(obj.prop);
}
3) 解构时重命名变量
function fun({ prop: newPropName }) {
console.log(newPropName);
}
等同于
function fun(obj) {
const newPropName = obj.prop;
console.log(newPropName);
}
4) 默认函数参数
function fun(arg = 5) {
console.log(arg);
}
fun(10); // prints 10
fun(); // prints 5 since no value was passed to fun
结论:
findOrCreateUser({ email: emailArg } = {}) {
// [...]
}
相当于
findOrCreateUser(args) {
const emailArg = args ? args.email : {};
// [...]
}
换句话说,它将传递给方法的对象的 email
属性 重命名为 emailArg
并使其直接可用。如果没有向函数传递任何内容,它还会将参数初始化为一个空对象。这是为了避免在没有传递任何内容的情况下引发 Cannot read email of undefined
。
如果您需要更多上下文,请参阅以下文档:
我认为:
1) 该函数希望接收一个 JavaScript 对象作为其第一个参数,该对象内部有一个名为 email
的 属性
2) 在函数内部,我们将 email
属性 称为 emailArg
最后,= {}
部分提供了一个空对象作为默认值,有助于避免错误。
可以在此处找到有关函数参数解构的更详细说明:https://davidwalsh.name/destructuring-function-arguments