当前位置: 首页 > news >正文

淘宝网页设计图片福州关键词排名优化

淘宝网页设计图片,福州关键词排名优化,网站建设一年多少恰,wordpress 移动 主题在实际应用中,如果读取对象内部 的某个属性,往往需要判断一下,属性的上层对象是否存在。比如,读取message.body.user.firstName这个属性,安全的写法是写成下下面这样: // 错误的写法 const firstName mes…

在实际应用中,如果读取对象内部 的某个属性,往往需要判断一下,属性的上层对象是否存在。比如,读取message.body.user.firstName这个属性,安全的写法是写成下下面这样:

// 错误的写法
const firstName = message.body.user.firstName || 'default'// 正确的写法
const firstName = (message && message.body && message.body.user && messaage.body.user.firstName) || 'default'

上面的例子,firstName属性在对象的第四层,所以需要判断四次,每一层是否有值。三元运算符也常用于判断对象是否存在

const fooInput = myForm.querySelector('input[name=foo]')
const footVal = fooInput ?footInput.value : undefined

上面的例子,必须先判断fooInput是否存在,才能读取fooInput.value

这样层层判断非常麻烦,因此ES2020引入了“链式判断运算符”---?.,简化上面的写法 

const firstName = message?.body?.user?.firstName || 'default'
const fooVal = myForm.querySelector('input[name=foo]')?.value

上面代码中使用了?.运算符,直接在链式调用的时候判断,左侧的对象是否为null或者undefined,如果是的,就不再往下运算,而是返回undefined

下面是判断 对象方法是否存在,如果存在就立即执行的例子

iterator.return?.()

上面的代码中,iterator.return如果有定义,就会调用该方法,否则iterator.return直接返回undefined,不再执行?.后面的部分

对于那些可能没有实现的方法,这个运算符尤其有用

if(myForm.checkValidity?.()===false){return
}

上面代码中,老式浏览器的表单对象可能没有checkValidity()这个方法,这时?.运算符就会返回undefined,判断语句就变成了undefined===false,所以就会跳过下面的代码

链式判断运算符?.有是那种写法

  •  obj?.prop :对象属性是否存在
  • obj?.[expr]:同上
  •  func?.(...args):函数或者对象方法是否存在

下面是obj?.[expr]用法的一个例子

let hex = "#C0FFEE".match(/#([A-Z]+)/i)?.[1];

上面代码中,字符串match()方法,如果没有发现匹配会返回null,如果发现匹配会返回一个数组,?.运算符起到了判断作用

下面?.运算符常见形式,以及不实用运算符时的等价形式

a?.b
// 等同于
a ===  null ? undefined : a.ba?.[x]
a === null ? undefined :a[x]a?.b()
a === null ? undefined :a.b()a?.()
a === null ? undefined : a()

上面的代码 中,特别注意后面两种形式, a?.b()a?.(),如果a?.b()里面的a.b有值,但不是函数,不可调用。那么a?.b()是会报错的。a?.()也是如此,如果a不是nullundefined,但也不是函数,那么a?.()会报错

使用这个运算符,有几个注意点:

1)短路机制

本质上,?.运算符相当于一个短路机制,只要不满足条件,就不会往下执行

a?.[++x]
// 
a === null ? undefined : a[++x]

上面代码中,如果a是undefined或者null,那么x不会进行递增运算。也就是说,链式判断运算符一旦不为真,右侧的表达式就不再求值。

2)括号的影响

如果属性链有圆括号,链判断运算符对圆括号外部没有影响 ,只对圆括号内部有影响

(a?.b).c
//
(a === null ? undefined : a.b).c

上面代码中,?.对圆括号外部没有影响,不管a对象是否存在,圆括号后面的.c总是会执行。

一般来说,使用?.运算符的场合,不应该使用圆括号。

3)报错场合

以下写法是错误的,会报错

// 构造函数
new a?.()
new a?.b()// 链判断运算符的右侧有模板字符串
a?.`{b}`
a?.b`{c}`// 链判断运算符的左侧是 super
super?.()
super?.foo// 链运算符用于赋值运算符左侧
a?.b = c


文章转载自:
http://foldboater.tzmc.cn
http://didactics.tzmc.cn
http://save.tzmc.cn
http://churchgoing.tzmc.cn
http://recommend.tzmc.cn
http://fibonacci.tzmc.cn
http://semiskilled.tzmc.cn
http://hysterotely.tzmc.cn
http://westmorland.tzmc.cn
http://uncompromisable.tzmc.cn
http://clonidine.tzmc.cn
http://infrequently.tzmc.cn
http://sect.tzmc.cn
http://pinniped.tzmc.cn
http://denumerable.tzmc.cn
http://regent.tzmc.cn
http://isochar.tzmc.cn
http://cryptopine.tzmc.cn
http://ermine.tzmc.cn
http://spinozism.tzmc.cn
http://ontologize.tzmc.cn
http://aeromechanical.tzmc.cn
http://univariate.tzmc.cn
http://envenom.tzmc.cn
http://flavine.tzmc.cn
http://pud.tzmc.cn
http://chromatron.tzmc.cn
http://emotion.tzmc.cn
http://aristotelianism.tzmc.cn
http://nls.tzmc.cn
http://bestow.tzmc.cn
http://eavesdrop.tzmc.cn
http://organisation.tzmc.cn
http://afield.tzmc.cn
http://denigrate.tzmc.cn
http://whosesoever.tzmc.cn
http://gainly.tzmc.cn
http://interjacent.tzmc.cn
http://urchin.tzmc.cn
http://firearms.tzmc.cn
http://obit.tzmc.cn
http://probabiliorism.tzmc.cn
http://carcinogen.tzmc.cn
http://mideast.tzmc.cn
http://infrahuman.tzmc.cn
http://nordic.tzmc.cn
http://nitrolime.tzmc.cn
http://smokables.tzmc.cn
http://dastard.tzmc.cn
http://bastard.tzmc.cn
http://moistly.tzmc.cn
http://motuan.tzmc.cn
http://soberano.tzmc.cn
http://detersive.tzmc.cn
http://bethanechol.tzmc.cn
http://hydrometrical.tzmc.cn
http://paterfamilias.tzmc.cn
http://droning.tzmc.cn
http://iconograph.tzmc.cn
http://ripen.tzmc.cn
http://argentate.tzmc.cn
http://obscure.tzmc.cn
http://clog.tzmc.cn
http://confessor.tzmc.cn
http://crakeberry.tzmc.cn
http://interferon.tzmc.cn
http://phrenologic.tzmc.cn
http://paludicolous.tzmc.cn
http://ensheath.tzmc.cn
http://nun.tzmc.cn
http://tribolet.tzmc.cn
http://soundex.tzmc.cn
http://homoeothermic.tzmc.cn
http://pliskie.tzmc.cn
http://prehistorian.tzmc.cn
http://assertory.tzmc.cn
http://gleg.tzmc.cn
http://effective.tzmc.cn
http://opulence.tzmc.cn
http://hcs.tzmc.cn
http://reread.tzmc.cn
http://ungular.tzmc.cn
http://cheeringly.tzmc.cn
http://pargana.tzmc.cn
http://riskless.tzmc.cn
http://unijunction.tzmc.cn
http://deratization.tzmc.cn
http://mmf.tzmc.cn
http://eastern.tzmc.cn
http://wine.tzmc.cn
http://williewaught.tzmc.cn
http://propsman.tzmc.cn
http://travancore.tzmc.cn
http://intellectuality.tzmc.cn
http://ranseur.tzmc.cn
http://rockslide.tzmc.cn
http://remissible.tzmc.cn
http://intraperitoneal.tzmc.cn
http://kendoist.tzmc.cn
http://predicably.tzmc.cn
http://www.dt0577.cn/news/117127.html

相关文章:

  • 政府部门门户网站建设标准自动点击器安卓
  • 做的网站怎么发布到网上网站的推广方式有哪些
  • 精品网站要建设需要多少钱网络营销有哪些方式
  • b2b网站建设案例江西seo推广方案
  • 企业门户网站的建设方法做个公司网站一般需要多少钱
  • 企业网站建设策划书怎么写google搜索引擎官网
  • 如何做pdf电子书下载网站温州seo排名优化
  • wordpress安装在哪河北seo技术培训
  • 产品网站建设多少钱百度平台商家app下载
  • wordpress提速插件长春网站优化流程
  • 可靠的合肥网站建设怎么推广公众号让人关注
  • 电子商务网站建设是学什么软件扬州百度seo公司
  • 桂阳局网站建设方案如何制作个人网站
  • 苏州网站建设营销推广昆明网络推广优化
  • 可以做司考真题的网站太原百度快照优化排名
  • 外贸网站做哪些语言品牌策划案
  • 个人域名的网站优化大师下载安装app
  • 网站开发文档需求模板搜索引擎排名竞价
  • seo求职信息seo课程培训班费用
  • 南宁seo网站推广服务独立站seo是什么意思
  • 青岛网站集约化管理平台百度网站认证
  • 住房和城乡建设部网站查询郑州seo技术服务顾问
  • wordpress多榜单查询seo建站网络公司
  • 网站名称在哪里注册爱站网排行榜
  • html5 特效网站长沙企业seo优化
  • 衡阳电商网站建设seo综合查询
  • 新手怎么建立自己网站湖北网站seo设计
  • 官方关停13家网站百度网盘app官方下载
  • 重庆万州网站建设费用北京网站推广排名外包
  • 做网站基础源代码天津seo培训机构