赞
踩
前后端联调时,我们有时会对后端传回的值进行判断,比如判断用户身份时:
- const judge = function(status){
- if(status === 0){
- return "普通用户"
- }else if(status === 1){
- return "管理员"
- }else if(status === 2){
- return "超级管理员"
- }else{
- return "未知用户"
- }
- }
ES6提供了一个更加方便的方法:
- const judge = function(status){
- const map = [
- 0: '普通用户',
- 1: '管理员',
- 2: '超级管理员'
- ]
-
- return map[status]??'未知用户'
- }
?? 是ES6提供的一个操作符,被称为非空运算符,例如 xxx?? ' yyy ' ,如果xxx的值不为null,则取xxx的值,如果xxx的值为null,则取yyy
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。