Object.prototype.lookupSetter()

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 实例的 __lookupSetter__() 方法返回作为指定属性的 setter 绑定的函数。

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

语法

¥Syntax

js
__lookupSetter__(prop)

参数

¥Parameters

prop

包含应返回其设置器的属性名称的字符串。

返回值

¥Return value

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

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

描述

¥Description

所有从 Object.prototype 继承的对象(即除 null-原型对象 之外的所有对象)都继承 __lookupSetter__() 方法。如果为对象的属性定义了 setter,则无法通过该属性引用 setter 函数,因为该属性仅在设置时调用该函数。__lookupSetter__() 可用于获取对 setter 函数的引用。

¥All objects that inherit from Object.prototype (that is, all except null-prototype objects) inherit the __lookupSetter__() method. If a setter has been defined for an object's property, it's not possible to reference the setter function through that property, because that property only calls the function when it's being set. __lookupSetter__() can be used to obtain a reference to the setter function.

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

¥__lookupSetter__() walks up the prototype chain to find the specified property. If any object along the prototype chain has the specified own property, the set 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.

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

¥__lookupSetter__() 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 __lookupSetter__(), it also needs to implement the __lookupGetter__(), __defineGetter__(), and __defineSetter__() methods.

示例

¥Examples

使用 lookupSetter()

¥Using __lookupSetter__()

js
const obj = {
  set foo(value) {
    this.bar = value;
  },
};

obj.__lookupSetter__("foo");
// [Function: set foo]

以标准方式查找属性的设置者

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

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

¥You should use the Object.getOwnPropertyDescriptor() API to look up a property's setter. Compared to __lookupSetter__(), 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 __lookupSetter__() method. If __lookupSetter__()'s behavior of walking up the prototype chain is important, you may implement it yourself with Object.getPrototypeOf().

js
const obj = {
  set foo(value) {
    this.bar = value;
  },
};

Object.getOwnPropertyDescriptor(obj, "foo").set;
// [Function: set foo]
js
const obj2 = {
  __proto__: {
    set foo(value) {
      this.bar = value;
    },
  },
};

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

console.log(findSetter(obj2, "foo")); // [Function: set foo]

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看