This feature is deprecated in favor of defining setters using the
object initializer syntax
或
Object.defineProperty()
API.
However, as it is widely implemented and used on the Web, it is very unlikely that browsers will stop implementing it.
__defineSetter__
method binds an object's property to a function to be called when an attempt is made to set that property.
obj.__defineSetter__(prop, fun)
prop
A string containing the name of the property to be bound to the given function.
fun
function(val) { . . . }
val
prop
.
__defineSetter__
method allows a
setter
to be defined on a pre-existing object.
var o = {};
o.__defineSetter__('value', function(val) { this.anotherValue = val; });
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
// Using the set operator
var o = { set value(val) { this.anotherValue = val; } };
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
// Using Object.defineProperty
var o = {};
Object.defineProperty(o, 'value', {
set: function(val) {
this.anotherValue = val;
}
});
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.prototype.__defineSetter__()' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
__defineSetter__
弃用
|
Chrome 1 | Edge 12 |
Firefox
1
|
IE 11 | Opera 9.5 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
弃用。不要用于新网站。
见实现注意事项。
Object.prototype.__defineGetter__()
set
operator
Object.defineProperty()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function