uvue.uts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. if(typeof obj == 'object') {
  18. const obj2 = {...toRaw(obj)}
  19. return obj2[key] != null
  20. }
  21. return false
  22. }
  23. export {
  24. hasOwn
  25. }
  26. // 示例
  27. // const obj = { name: 'John', age: 30 };
  28. // if (hasOwn(obj, 'name')) {
  29. // console.log("对象具有 'name' 属性");
  30. // } else {
  31. // console.log("对象不具有 'name' 属性");
  32. // }
  33. // // 输出: 对象具有 'name' 属性
  34. // const arr = [1, 2, 3];
  35. // if (hasOwn(arr, 'length')) {
  36. // console.log("数组具有 'length' 属性");
  37. // } else {
  38. // console.log("数组不具有 'length' 属性");
  39. // }
  40. // 输出: 数组具有 'length' 属性