要获取javascript原型链上的getter方法,必须沿原型链向上查找,使用object.getprototypeof和object.getownpropertydescriptor;对于symbol类型,需通过object.getownpropertysymbols遍历symbol属性匹配目标;不推荐使用已废弃的__lookupgetter__;若getter可能抛出错误,应使用try...catch安全调用。1. 使用getgetterfromprototypechain函数遍历原型链,通过object.getownpropertydescriptor检测属性描述符中的get字段,找到则返回getter;2. 对于symbol类型的getter,使用getsymbolgetterfromprototypechain函数,先用object.getownpropertysymbols获取对象自身的symbol属性,再逐一比对并检查其描述符的get字段;3. 避免使用__lookupgetter__,因其为非标准且已被废弃;4. 在调用getter时使用safegetgettervalue函数包裹try...catch,捕获可能的执行错误并返回默认值,确保程序健壮性。该方法完整实现了对普通属性和symbol属性的getter查找与安全调用。
获取JavaScript原型链上的getter方法,需要沿着对象的原型链向上查找,直到找到目标getter或者到达原型链的顶端(
null
Object.getPrototypeOf()
Object.getOwnPropertyDescriptor()
解决方案
function getGetterFromPrototypeChain(obj, propertyName) { let current = obj; while (current) { const descriptor = Object.getOwnPropertyDescriptor(current, propertyName); if (descriptor && descriptor.get) { return descriptor.get; } current = Object.getPrototypeOf(current); } return undefined; // 没有找到 getter } // 示例 const myObject = { value: 10, get doubleValue() { return this.value * 2; } }; const inheritedObject = Object.create(myObject); const getter = getGetterFromPrototypeChain(inheritedObject, 'doubleValue'); if (getter) { console.log(getter.call(inheritedObject)); // 输出 20 } else { console.log("没有找到 getter"); }
这段代码首先定义了一个函数
getGetterFromPrototypeChain
while
Object.getOwnPropertyDescriptor
get
undefined
如何处理Symbol类型的getter?
Symbol类型的属性需要特别处理,因为它们不是字符串,不能直接用字符串字面量访问。需要使用
Object.getOwnPropertySymbols()
function getSymbolGetterFromPrototypeChain(obj, symbol) { let current = obj; while (current) { const symbols = Object.getOwnPropertySymbols(current); for (const sym of symbols) { if (sym === symbol) { const descriptor = Object.getOwnPropertyDescriptor(current, sym); if (descriptor && descriptor.get) { return descriptor.get; } } } current = Object.getPrototypeOf(current); } return undefined; } // 示例 const mySymbol = Symbol('myGetter'); const myObject = { [mySymbol]: 10, get [mySymbol]() { return this[mySymbol] * 2; } }; const inheritedObject = Object.create(myObject); const getter = getSymbolGetterFromPrototypeChain(inheritedObject, mySymbol); if (getter) { console.log(getter.call(inheritedObject)); // 输出 NaN,因为 inheritedObject 没有 [mySymbol] 属性 } else { console.log("没有找到 getter"); }
这个函数
getSymbolGetterFromPrototypeChain
Object.getOwnPropertySymbols
为什么不直接用obj.__lookupGetter__(propertyName)
__lookupGetter__
Object.getOwnPropertyDescriptor
Object.getPrototypeOf
如果getter抛出错误,如何处理?
在调用getter时,需要考虑getter可能抛出错误的情况。可以使用
try...catch
function safeGetGetterValue(obj, propertyName) { const getter = getGetterFromPrototypeChain(obj, propertyName); if (getter) { try { return getter.call(obj); } catch (error) { console.error(`调用 getter 出错: ${error}`); return undefined; // 或者返回其他默认值,取决于你的需求 } } return undefined; } // 示例 const myObject = { value: 10, get dangerousValue() { throw new Error("故意抛出的错误"); } }; const result = safeGetGetterValue(myObject, 'dangerousValue'); console.log(result); // 输出 undefined,并打印错误信息
这个函数
safeGetGetterValue
try...catch
catch
undefined
以上就是js怎么获取原型链上的getter方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号