翻译正在进行中。
概述
set 语法可以将一个函数绑定在当前对象的指定属性上,当那个属性被赋值时,你所绑定的函数就会被调用。
语法
{set prop(val) { . . . }}
{set [expression](val) { . . . }}
参数
-
prop -
将被指定函数绑定的属性名。
-
val -
An alias for the variable that holds the value attempted to be assigned to
prop. - expression
- Starting with ECMAScript 6, you can also use expressions for a computed property name to bind to the given function.
描述
In JavaScript, a setter can be used to execute a function whenever a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.
Note the following when working with the set syntax:
- It can have an identifier which is either a number or a string;
- It must have exactly one parameter (see Incompatible ES5 change: literal getter and setter functions must now have exactly zero or one arguments for more information);
- It must not appear in an object literal with another
setor with a data entry for the same property.
({ set x(v) { }, set x(v) { } }and{ x: ..., set x(v) { } }are forbidden )
A setter can be removed using the delete operator.
示例
在对象初始化时定义 setter
This will define a pseudo-property current of object o that, when assigned a value, will update log with that value:
var o = {
set current (str) {
return this.log[this.log.length] = str;
},
log: []
}
请注意,current 属性是未定义的,访问它时将会返回 undefined。
用 delete 操作符移除一个 setter
我们可以使用 delete 操作符移除 setter。
delete o.current;
使用 defineProperty 为已存在的对象定义 setter
我们可以随时使用 Object.defineProperty() 给一个已经存在的对象添加一个 setter。
var o = { a:0 };
Object.defineProperty(o, "b", { set: function (x) { this.a = x / 2; } });
o.b = 10; // Runs the setter, which assigns 10 / 2 (5) to the 'a' property
console.log(o.a) // 5
使用 computed 属性名
注意: Computed 属性是个实验性技术,属于 ECMAScript 6 提案的内容,现在它尚未广泛得到浏览器厂商的支持。在尚未支持 computed 属性的浏览器中使用时将会抛出一个语法错误。
var expr = "foo";
var obj = {
baz: "bar",
set [expr](v) { this.baz = v; }
};
console.log(obj.baz); // "bar"
obj.foo = "baz"; // run the setter
console.log(obj.baz); // "baz"
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) Object Initializer |
Standard | Initial definition. |
| ECMAScript 6 (ECMA-262) Method definitions |
Draft | Added computed property names. |
浏览器兼容性
根据 Robert Nyman's page 显示,在不支持 setter 的浏览器上(尤其是 IE6-8),将会抛出一个语法错误。
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 1 | 2.0 (1.8.1) | 9 | 9.5 | 3 |
| Computed property names | Not supported | 34 (34) | Not supported | Not supported | Not supported |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 1.0 (1.8.1) | (Yes) | (Yes) | (Yes) |
| Computed property names | Not supported | Not supported | 34.0 (34.0) | Not supported | Not supported | Not supported |
SpiderMonkey 用户注意
- 从 JavaScript 1.8.1 开始 ,在设置对象属性和数组初始化时,setters 将不会被调用。
相关链接
- getter
deleteObject.defineProperty()__defineGetter____defineSetter__- Defining Getters and Setters in JavaScript Guide