Web/JavaScript/Reference/Global objects/Object/ defineSetter
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.
Syntax
obj.__defineSetter__(prop, fun)
Parameters
prop- A string containing the name of the property to be bound to the given function.
funA function to be called when there is an attempt to set the specified property. This function takes the form
function(val) { . . . }val- An alias for the variable that holds the value attempted to be assigned to
prop.
Return value
Description
The __defineSetter__ method allows a setter to be defined on a pre-existing object.
Examples
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
Specifications
| Specification |
|---|
| ECMAScript (ECMA-262)The definition of 'Object.prototype.__defineSetter__()' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox Full support 1 Full support 1 Notes' Starting with Firefox 48, this method can no longer be called at the global scope without any object. A |
IE
Full support 11 |
Opera
Full support 9.5 |
Safari
Full support 3 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
nodejs
Full support Yes |
Legend
- Full support
- Full support
- Deprecated. Not for use in new websites.'
- Deprecated. Not for use in new websites.
- See implementation notes.'
- See implementation notes.
See also
Object.prototype.__defineGetter__()setoperatorObject.defineProperty()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()- JS Guide: Defining Getters and Setters
- [Blog Post Deprecation of __defineGetter__ and __defineSetter__]
- bug 647423
Object.prototype.__defineSetter__() by Mozilla Contributors is licensed under CC-BY-SA 2.5.