This feature is deprecated in favor of defining setters using the object initializer syntax or the Object.defineProperty() API.
However, as it is widely implemented and used on the Web, it is very unlikely that browsers will stop implementing it.
The __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)
propfunfunction(val) { . . . } valprop.The __defineSetter__ method allows a setter to be defined on a pre-existing object.
// Non-standard and deprecated way
var o = {};
o.__defineSetter__('value', function(val) { this.anotherValue = val; });
o.value = 5;
console.log(o.value); // undefined
console.log(o.anotherValue); // 5
// Standard-compliant ways
// 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
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) The definition of 'Object.prototype.__defineSetter__()' in that specification. | Draft | Included in the (normative) annex for additional ECMAScript legacy features for Web browsers (note that the specification codifies what is already in implementations). |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | Yes | Yes | 1
|
11 | Yes | Yes |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | Yes | Yes | 4 | Yes | Yes | Yes |
| Server | |
|---|---|
| Node.js | |
| Basic support | Yes |
Object.prototype.__defineGetter__()set operatorObject.defineProperty()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineSetter__