uvue.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @ts-nocheck
  2. /**
  3. * 检查对象或数组是否具有指定的属性或键
  4. * @param obj 要检查的对象或数组
  5. * @param key 指定的属性或键
  6. * @returns 如果对象或数组具有指定的属性或键,则返回true;否则返回false
  7. */
  8. function hasOwn(obj: UTSJSONObject, key: string): boolean
  9. function hasOwn(obj: Map<string, unknown>, key: string): boolean
  10. function hasOwn(obj: any, key: string): boolean {
  11. if(obj instanceof UTSJSONObject){
  12. return obj[key] != null
  13. }
  14. if(obj instanceof Map<string, unknown>){
  15. return (obj as Map<string, unknown>).has(key)
  16. }
  17. return false
  18. }
  19. export {
  20. hasOwn
  21. }
  22. // 示例
  23. // const obj = { name: 'John', age: 30 };
  24. // if (hasOwn(obj, 'name')) {
  25. // console.log("对象具有 'name' 属性");
  26. // } else {
  27. // console.log("对象不具有 'name' 属性");
  28. // }
  29. // // 输出: 对象具有 'name' 属性
  30. // const arr = [1, 2, 3];
  31. // if (hasOwn(arr, 'length')) {
  32. // console.log("数组具有 'length' 属性");
  33. // } else {
  34. // console.log("数组不具有 'length' 属性");
  35. // }
  36. // 输出: 数组具有 'length' 属性