Object.prototype.lookupGetter()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

注意:此功能已被弃用,取而代之的是 Object.getOwnPropertyDescriptor() API。此方法的行为仅为 Web 兼容性而指定,不需要在任何平台中实现。它可能并非在所有地方都有效。

¥Note: This feature is deprecated in favor of the Object.getOwnPropertyDescriptor() API. This method's behavior is only specified for web compatibility, and is not required to be implemented in any platform. It may not work everywhere.

Object 实例的 __lookupGetter__() 方法返回作为 getter 绑定到指定属性的函数。

¥The __lookupGetter__() method of Object instances returns the function bound as a getter to the specified property.

语法

¥Syntax

js
__lookupGetter__(prop)

参数

¥Parameters

prop

包含应返回其 getter 的属性名称的字符串。

返回值

¥Return value

该函数作为 getter 绑定到指定的属性。如果未找到此类属性,或者该属性是 数据属性,则返回 undefined

¥The function bound as a getter to the specified property. Returns undefined if no such property is found, or the property is a data property.

描述

¥Description

所有从 Object.prototype 继承的对象(即除 null-原型对象 之外的所有对象)都继承 __lookupGetter__() 方法。如果为对象的属性定义了 getter,则无法通过该属性引用 getter 函数,因为该属性引用该函数的返回值。__lookupGetter__() 可用于获取对 getter 函数的引用。

¥All objects that inherit from Object.prototype (that is, all except null-prototype objects) inherit the __lookupGetter__() method. If a getter has been defined for an object's property, it's not possible to reference the getter function through that property, because that property refers to the return value of that function. __lookupGetter__() can be used to obtain a reference to the getter function.

__lookupGetter__() 沿着 原型链 向上查找指定的属性。如果原型链上的任何对象具有指定的 自有属性,则返回该属性的 属性描述符get 属性。如果该属性是数据属性,则返回 undefined。如果在整个原型链上都没有找到该属性,则也返回 undefined

¥__lookupGetter__() walks up the prototype chain to find the specified property. If any object along the prototype chain has the specified own property, the get attribute of the property descriptor for that property is returned. If that property is a data property, undefined is returned. If the property is not found along the entire prototype chain, undefined is also returned.

__lookupGetter__() 在规范中定义为 "规范性 可选性",这意味着不需要实现即可实现此目的。然而,所有主流浏览器都实现了它,并且由于它的持续使用,它不太可能被删除。如果浏览器实现了 __lookupGetter__(),它还需要实现 __lookupSetter__()__defineGetter__()__defineSetter__() 方法。

¥__lookupGetter__() is defined in the spec as "normative optional", which means no implementation is required to implement this. However, all major browsers implement it, and due to its continued usage, it's unlikely to be removed. If a browser implements __lookupGetter__(), it also needs to implement the __lookupSetter__(), __defineGetter__(), and __defineSetter__() methods.

示例

¥Examples

使用 lookupGetter()

¥Using __lookupGetter__()

js
const obj = {
  get foo() {
    return Math.random() > 0.5 ? "foo" : "bar";
  },
};

obj.__lookupGetter__("foo");
// [Function: get foo]

以标准方式查找属性的 getter

¥Looking up a property's getter in the standard way

你应该使用 Object.getOwnPropertyDescriptor() API 来查找属性的 getter。与 __lookupGetter__() 相比,此方法允许查找 symbol 属性。Object.getOwnPropertyDescriptor() 方法也适用于 null-原型对象null-原型对象 不是从 Object.prototype 继承的,因此没有 __lookupGetter__() 方法。如果 __lookupGetter__() 沿着原型链向上的行为很重要,你可以用 Object.getPrototypeOf() 自己实现它。

¥You should use the Object.getOwnPropertyDescriptor() API to look up a property's getter. Compared to __lookupGetter__(), this method allows looking up symbol properties. The Object.getOwnPropertyDescriptor() method also works with null-prototype objects, which don't inherit from Object.prototype and therefore don't have the __lookupGetter__() method. If __lookupGetter__()'s behavior of walking up the prototype chain is important, you may implement it yourself with Object.getPrototypeOf().

js
const obj = {
  get foo() {
    return Math.random() > 0.5 ? "foo" : "bar";
  },
};

Object.getOwnPropertyDescriptor(obj, "foo").get;
// [Function: get foo]
js
const obj2 = {
  __proto__: {
    get foo() {
      return Math.random() > 0.5 ? "foo" : "bar";
    },
  },
};

function findGetter(obj, prop) {
  while (obj) {
    const desc = Object.getOwnPropertyDescriptor(obj, prop);
    if (desc) {
      return desc.get;
    }
    obj = Object.getPrototypeOf(obj);
  }
}

console.log(findGetter(obj2, "foo")); // [Function: get foo]

规范

Specification
ECMAScript Language Specification
# sec-object.prototype.__lookupGetter__

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看