Object.prototype.defineSetter()
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.defineProperty()
API 定义 setters。此方法的行为仅为 Web 兼容性而指定,不需要在任何平台中实现。它可能并非在所有地方都有效。¥Note: This feature is deprecated in favor of defining setters using the object initializer syntax or the
Object.defineProperty()
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
实例的 __defineSetter__()
方法将对象的属性绑定到在尝试设置该属性时调用的函数。
¥The __defineSetter__()
method of Object
instances binds an object's property to a function to be called when an attempt is made to set that property.
语法
参数
返回值
例外情况
描述
¥Description
所有从 Object.prototype
继承的对象(即除 null
-原型对象 之外的所有对象)都继承 __defineSetter__()
方法。此方法允许在预先存在的对象上定义 setter。这相当于 Object.defineProperty(obj, prop, { set: func, configurable: true, enumerable: true })
,这意味着该属性是可枚举和可配置的,并且任何现有的 getter(如果存在)都会被保留。
¥All objects that inherit from Object.prototype
(that is, all except null
-prototype objects) inherit the __defineSetter__()
method. This method allows a setter to be defined on a pre-existing object. This is equivalent to Object.defineProperty(obj, prop, { set: func, configurable: true, enumerable: true })
, which means the property is enumerable and configurable, and any existing getter, if present, is preserved.
__defineSetter__()
在规范中定义为 "规范性 可选性",这意味着不需要实现即可实现此目的。然而,所有主流浏览器都实现了它,并且由于它的持续使用,它不太可能被删除。如果浏览器实现了 __defineSetter__()
,它还需要实现 __lookupGetter__()
、__lookupSetter__()
和 __defineGetter__()
方法。
¥__defineSetter__()
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 __defineSetter__()
, it also needs to implement the __lookupGetter__()
, __lookupSetter__()
, and __defineGetter__()
methods.
示例
使用 defineSetter()
以标准方式定义 setter 属性
¥Defining a setter property in standard ways
你可以在对象首次初始化时使用 set
语法来定义 setter。
¥You can use the set
syntax to define a setter when the object is first initialized.
const o = {
set value(val) {
this.anotherValue = val;
},
};
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
你还可以在创建对象后使用 Object.defineProperty()
在对象上定义 setter。与 __defineSetter__()
相比,此方法允许你控制 setter 的可枚举性和可配置性,以及定义 symbol 属性。Object.defineProperty()
方法也适用于 null
-原型对象,null
-原型对象 不是从 Object.prototype
继承的,因此没有 __defineSetter__()
方法。
¥You may also use Object.defineProperty()
to define a setter on an object after it's been created. Compared to __defineSetter__()
, this method allows you to control the setter's enumerability and configurability, as well as defining symbol properties. The Object.defineProperty()
method also works with null
-prototype objects, which don't inherit from Object.prototype
and therefore don't have the __defineSetter__()
method.
const o = {};
Object.defineProperty(o, "value", {
set(val) {
this.anotherValue = val;
},
configurable: true,
enumerable: true,
});
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
规范
Specification |
---|
ECMAScript Language Specification # sec-object.prototype.__defineSetter__ |
浏览器兼容性
BCD tables only load in the browser