--- title: Object.prototype.__lookupGetter__() slug: Web/JavaScript/Reference/Global_Objects/Object/__lookupGetter__ translation_of: Web/JavaScript/Reference/Global_Objects/Object/__lookupGetter__ ---
__lookupGetter__
方法会返回当前对象上指定属性的属性读取访问器函数(getter)。
obj.__lookupGetter__(sprop)
sprop
var obj = { get foo() { return Math.random() > 0.5 ? "foo" : "bar"; } }; obj.__lookupGetter__("foo") // (function (){return Math.random() > 0.5 ? "foo" : "bar"})
__lookupGetter__
方法是非标准的,我们应该使用标准中定义的方法来完成同样的事情,那就是 {{jsxref("Object.getOwnPropertyDescriptor()")}} 方法:
var obj = { get foo() { return Math.random() > 0.5 ? "foo" : "bar"; } }; Object.getOwnPropertyDescriptor(obj, "foo").get // (function (){return Math.random() > 0.5 ? "foo" : "bar"})
如果那个访问器属性是继承来的,你还需要使用 {{jsxref("Object.getPrototypeOf()")}}:
var obj = {}; var prototype = Object.getPrototypeOf(obj); Object.getOwnPropertyDescriptor(prototype, "foo").get // function __proto__() {[native code]}
不属于任何规范。