We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
左侧的对象是否为null或undefined,若是则不再往下运算,而是返回undefined,否则一直运算下去,直到返回最后一个值。 链判断运算符?.读取深度嵌套在对象链中的属性值,而不必验证每个值。当值为空时,表达式停止计算并返回 undefined。
let person = { name: '拾柒', body: { color: 'yellow', height: 179, weight: 70, sex: null }, action: function(name){ console.log(name) } } let res = person?.body?.age; // => undefined // 等价于 let res = person&& person.body&& person.body.location // 作用就是判断person下的body下的age是否为null或者undefined,当其中一链为null或者undefined时就直接返回undefined let res2 = person?.sex; // => undefined
链判断运算符?.有三种写法。
person ?. action ?. ('peter'); // peter
忽略错误值(如 0 和空字符串)的同时指定默认值。 左侧的对象是否为 undefined 或 null,若是的话,直接返回下一个值,若不是的话,则直接返回前一个值。 判断方式和 || 一样 区别就是:
function query(item) { return item ?? {} } // 等价于 function query(item) { if (item == null || item == undefined) { return {} } else { return item; } }
function query(item) { return item || {} } // 等价于 function query(item) { if (item === 0 || item === "" || item === false || item === null || item === undefined) { return {} } else { return item; } }
仅当值为 null 或 undefined 时,此赋值运算符才会赋值(没想出来和??比有什么适用场景...)
function config(options) { options.duration ??= 100; options.speed ??= 25; return options; } config({ duration: 125 }); // { duration: 125, speed: 25 } config({}); // { duration: 100, speed: 25 }
当前者不是假值时,可赋值后者的值,若是假值时,直接赋值假值
let A = 'abc'; let a = 1; let b = 0; let c = false; let d = null; let e = undefined; let f = ''; A &&= '拾柒'; // '拾柒' a &&= '拾柒'; // '拾柒' b &&= '拾柒'; // 0 c &&= '拾柒'; // false d &&= '拾柒'; // null e &&= '拾柒'; // undefined f &&= '拾柒'; // ''
用来做类型判断,在第一步!(变量)之后再做逻辑取反运算
var a; if(a != null && typeof(a) != undefined && a != ''){ //a有内容才执行的代码 }
等价于
if(!!a){ //a有内容才执行的代码... }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
?. 链判断运算符
左侧的对象是否为null或undefined,若是则不再往下运算,而是返回undefined,否则一直运算下去,直到返回最后一个值。
链判断运算符?.读取深度嵌套在对象链中的属性值,而不必验证每个值。当值为空时,表达式停止计算并返回 undefined。
链判断运算符?.有三种写法。
?? 空值合并运算符
忽略错误值(如 0 和空字符串)的同时指定默认值。
左侧的对象是否为 undefined 或 null,若是的话,直接返回下一个值,若不是的话,则直接返回前一个值。
判断方式和 || 一样
区别就是:
??= 空赋值运算符
仅当值为 null 或 undefined 时,此赋值运算符才会赋值(没想出来和??比有什么适用场景...)
&&= 与赋值运算符
当前者不是假值时,可赋值后者的值,若是假值时,直接赋值假值
!! 双非
用来做类型判断,在第一步!(变量)之后再做逻辑取反运算
等价于
The text was updated successfully, but these errors were encountered: