微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

问号在JS的return语句中是什么意思?

如何解决问号在JS的return语句中是什么意思?

我有一个函数服务,使用加载程序中的数据,并以正确的方式对其进行转换,然后返回新数据 我建议使用“?”在转换之前返回数据,如果没有来自加载程序的数据,这可能会很有意义:


export default async function serviceInputSearch(url) {
  const data = await new DataLoader(url).get();
  return data?.data.results;
}

我在Google中找不到有关此“?”的任何信息。在回报声明中? 什么意思?

解决方法

这称为可选链接。您可以在这里找到有关此信息的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

但是,在您的示例中,要点是问号可以验证是否存在有效的“数据”对象。如果您那里没有问号,并且没有数据对象或它为null,则在“无法读取'undefined'的属性数据”行中将引发错误。

,

这称为 Optional chaining

const adventurer = {
  name: 'Alice',cat: {
    name: 'Dinah'
  }
};

// you can use it to check if the property is exist in the object
const dogName = adventurer.dog?.name; //same as adventurer.dog && adventurer.dog.name ? adventurer.dog.name : undefined;
console.log(dogName); //undefined

否则您可以将其用于检查/调用对象内部功能的存在性

// if someNonExistentMethod is exists in the adventurer Object call it.
console.log(adventurer.someNonExistentMethod?.());

语法是

const adventurer = {
  name: 'Alice',cat: {
    name: 'Dinah'
  },skills: ['jump']
};

obj.val?.prop => //Optional chaining
    ex: adventurer.dog?.name

obj.val?.[expr] => //Optional chaining with expressions
    ex: const propertyName = 'name'; adventurer.dog?.[propertyName];

obj.arr?.[index] => //Array item access with optional chaining
    ex: adventurer.skills?.[0] //jump

obj.func?.(args) => //Optional chaining with function calls
    ex: adventurer.someNonExistentMethod?.()
,

它是“可选链接”运算符。

这里是一个用例:

let obj = {};

console.log(obj?.person?.age);
console.log(obj.person.age);

如果您尝试访问不存在的属性,它将很方便。其undefined,因此您会收到错误cannot get xx of undefined

为防止此错误,请在其前面放置一个?.,它将返回一个undefined而不是抛出错误

这里有一些例子:

let obj = {};


//method does not exist
console.log(obj.func?.())


let arr = ["tom"];
console.log(arr[2]?.name);


let matrix = [ [ 1 ],[ 2 ] ];
console.log(matrix[5]?.[3]);


let defaultName = obj?.name ?? "Tom";
console.log(defaultName);

,

它是可选的链接运算符。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

,

reference

在Javascript中,我们需要显式检查每个引用以确保已定义。如果我们在一个对象中有一个嵌套很深的引用,它可能看起来像这样:

let networkFetchResult = await fetch("https://myfriends.com/user/123").then(res => res.json());
// Let's assume that the first friend of our request user is our best friend
let bestFriendsName = networkFetchResult
  && networkFetchResult.data
  && networkFetchResult.data.user
  && networkFetchResult.data.user.friends
  && networkFetchResult.data.user.friends[0]
  && networkFetchResult.data.user.friends[0].name
  || "You don't have any friends!";

相反,您只能这样做

let networkFetchResult = await fetch("https://myfriends.com/user/123").then(res => res.json());
// Still assuming that our first friend is our best friend
let bestFriendsName = networkFetchResult?.data?.user?.friends?.[0]?.name || "You don't have any friends!";

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。