cxc-szcx-uniapp/uni_modules/lime-shared/hasOwn/vue.ts

30 lines
886 B
TypeScript
Raw Normal View History

2025-03-06 18:35:00 +00:00
// @ts-nocheck
const hasOwnProperty = Object.prototype.hasOwnProperty
/**
*
* @param obj
* @param key
* @returns truefalse
*/
export function hasOwn(obj: Object | Array<any>, key: string): boolean {
return hasOwnProperty.call(obj, key);
}
// 示例
// const obj = { name: 'John', age: 30 };
// if (hasOwn(obj, 'name')) {
// console.log("对象具有 'name' 属性");
// } else {
// console.log("对象不具有 'name' 属性");
// }
// // 输出: 对象具有 'name' 属性
// const arr = [1, 2, 3];
// if (hasOwn(arr, 'length')) {
// console.log("数组具有 'length' 属性");
// } else {
// console.log("数组不具有 'length' 属性");
// }
// 输出: 数组具有 'length' 属性