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
A 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 .

返回值

undefined .

描述

__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
								

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.prototype.__defineSetter__()' in that specification.

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
__defineSetter__ 弃用 Chrome 1 Edge 12 Firefox 1
1
Starting with Firefox 48, this method can no longer be called at the global scope without any object. A TypeError will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.
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

图例

完整支持

完整支持

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Object
  3. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  4. 方法
    1. Object.assign()
    2. Object.create()
    3. Object.defineProperties()
    4. Object.defineProperty()
    5. Object.entries()
    6. Object.freeze()
    7. Object.fromEntries()
    8. Object.getOwnPropertyDescriptor()
    9. Object.getOwnPropertyDescriptors()
    10. Object.getOwnPropertyNames()
    11. Object.getOwnPropertySymbols()
    12. Object.getPrototypeOf()
    13. Object.is()
    14. Object.isExtensible()
    15. Object.isFrozen()
    16. Object.isSealed()
    17. Object.keys()
    18. Object.preventExtensions()
    19. Object.prototype.__defineGetter__()
    20. Object.prototype.__defineSetter__()
    21. Object.prototype.__lookupGetter__()
    22. Object.prototype.__lookupSetter__()
    23. Object.prototype.hasOwnProperty()
    24. Object.prototype.isPrototypeOf()
    25. Object.prototype.propertyIsEnumerable()
    26. Object.prototype.toLocaleString()
    27. Object.prototype.toSource()
    28. Object.prototype.toString()
    29. Object.prototype.valueOf()
    30. Object.seal()
    31. Object.setPrototypeOf()
    32. Object.values()
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1