js中逻辑或的作用
程序员文章站
2022-06-24 11:07:56
1 var config = null; 2 undefined 3 var result = config||{}; 4 undefined 5 result 6 {} 7 8 var config = undefined; 9 undefined 10 var result = config |... ......
1 var config = null; 2 undefined 3 var result = config||{}; 4 undefined 5 result 6 {} 7 8 var config = undefined; 9 undefined 10 var result = config || {}; 11 undefined 12 result 13 {} 14 15 var config = ''; 16 undefined 17 var result = config||{}; 18 undefined 19 result 20 {} 21 22 var config = 0; 23 undefined 24 var result = config||{}; 25 undefined 26 result 27 {} 28 29 var config = false; 30 undefined 31 var result = config || {}; 32 undefined 33 result 34 {} 35 36 37 38 var config = {name:'pine'};//'0' '1' true 39 undefined 40 var result = config||{}; 41 undefined 42 result 43 {name: "pine"} 44 45 46 var result = config||{}; 47 ||在此例中的作用: 48 如果config为null、undefined、''、0、false,则表达式返回{}, 49 否则表达式返回config 50 51 等同于: 52 function nvl(config){ 53 if(config==null||config==undefined||config===''|| 54 config===0||config===false){ 55 return {}; 56 } 57 return config; 58 }