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