1
0

vue.ts 886 B

123456789101112131415161718192021222324252627282930
  1. // @ts-nocheck
  2. const hasOwnProperty = Object.prototype.hasOwnProperty
  3. /**
  4. * 检查对象或数组是否具有指定的属性或键
  5. * @param obj 要检查的对象或数组
  6. * @param key 指定的属性或键
  7. * @returns 如果对象或数组具有指定的属性或键,则返回true;否则返回false
  8. */
  9. export function hasOwn(obj: Object | Array<any>, key: string): boolean {
  10. return hasOwnProperty.call(obj, key);
  11. }
  12. // 示例
  13. // const obj = { name: 'John', age: 30 };
  14. // if (hasOwn(obj, 'name')) {
  15. // console.log("对象具有 'name' 属性");
  16. // } else {
  17. // console.log("对象不具有 'name' 属性");
  18. // }
  19. // // 输出: 对象具有 'name' 属性
  20. // const arr = [1, 2, 3];
  21. // if (hasOwn(arr, 'length')) {
  22. // console.log("数组具有 'length' 属性");
  23. // } else {
  24. // console.log("数组不具有 'length' 属性");
  25. // }
  26. // 输出: 数组具有 'length' 属性