1/* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 3 */ 4 5/** 6 * Object工具类 7 */ 8namespace ObjectUtil { 9 10/** 11 * 判断是否为null 12 */ 13 export function isNull(obj: any): boolean { 14 return obj === null; 15 } 16 /** 17 * 判断是否为undefined 18 * @param obj 19 */ 20 export function isUndefined(obj: any): boolean { 21 return obj === undefined; 22 } 23 /** 24 * 判断是否为null 或者 undefined 25 * @param obj 26 */ 27 export function isNullOrUndefined(obj: any): boolean { 28 return isNull(obj) || isUndefined(obj); 29 } 30 31 /** 32 * 返回string,如果是null or undefined返回defaultValue 33 */ 34 export function toString(obj: any, defaultValue: string = ''): string { 35 if (this.isNullOrUndefined(obj)) { 36 return defaultValue; 37 } else { 38 return obj.toString(); 39 } 40 } 41 42 /** 43 * 判断对象中是否有某个属性 44 * @param obj 校验对象 45 * @param key 校验属性 46 */ 47 export function hasKey(obj: object, key: string): boolean { 48 return Object.prototype.hasOwnProperty.call(obj, key); 49 } 50} 51 52export default ObjectUtil;