xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js ?. operator All In One

js ?. operator All In One

js 可选链操作符 ?.

obj.prop?.name;

const obj = {
  name: 'abc',
  cat: {
    name: 'xyz'
  },
  bug: {
    alias: 'ufo'
  }
};

const dogName = obj.dog?.name;

console.log(dogName);
// undefined

const name = obj.dog.name;
console.log(name);
// Error: Cannot read property 'name' of undefined

refs

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



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-01-31 14:25  xgqfrms  阅读(73)  评论(0编辑  收藏  举报