2022-10-04 15:57阅读: 162评论: 0推荐: 0

前端js操作符: ?/?? /!!用法

前言

最近大概浏览别人源码的过程中发现有些操作符我竟然从来没用过。经过查询做个笔记。

操作符

这个应该很好理解,就是简单的三元操作符

 例子: a ? b : c  // 表示a===true 执行b否则执行c 

??

a ?? b // a为null或undefined才返回b,否则返回a

a ?? = b // 变量a的值为null或undefined时才等于变量b的值,否则返回a

?.

obj.a?.b?.c
// 等价于
if (obj.a && obj.a.b) {
  // 获得obj.a.b.c
}

!!

var a;
if(a!=null&&typeof(a)!=undefined&&a!=''){
    //a有内容才执行的代码  
}
if(!!a){
    //a有内容才执行的代码...  
}

!.

这是TypeScript的语法,叫非空断言操作符(non-null assertion operator),和?.相反,这个符号表示对象后面的属性一定不是null或undefined。

文档地址:https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-

原文地址:

https://ligo100.cn/?p=337&preview=trueicon-default.png?t=M85Bhttps://ligo100.cn/?p=337&preview=true

本文作者:不随。

本文链接:https://www.cnblogs.com/tmblog/p/17849263.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   不随。  阅读(162)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.