1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
---
title: setter
slug: Web/JavaScript/Reference/Functions/set
tags:
- ECMAScript 5
- Functions
- JavaScript
translation_of: Web/JavaScript/Reference/Functions/set
---
<div>{{jsSidebar("Functions")}}</div>
<p>当尝试设置属性时,<strong><code>set</code></strong>语法将对象属性绑定到要调用的函数。</p>
<div>{{EmbedInteractiveExample("pages/js/functions-setter.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">{set <em>prop</em>(<em>val</em>) { . . . }}
{set [expression](<em>val</em>) { . . . }}</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>prop</code></dt>
<dd>要绑定到给定函数的属性名。</dd>
</dl>
<dl>
<dt><code>val</code></dt>
<dd>用于保存尝试分配给<code>prop</code>的值的变量的一个别名。</dd>
<dt>表达式</dt>
<dd>从 ECMAScript 2015 开始,还可以使用一个计算属性名的表达式绑定到给定的函数。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>在 javascript 中,如果试着改变一个属性的值,那么对应的 setter 将被执行。setter 经常和 getter 连用以创建一个伪属性。不可能在具有真实值的属性上同时拥有一个 setter 器。</p>
<p>使用 set 语法时请注意:</p>
<div>
<ul>
<li>它的标识符可以是数字或字符串;</li>
<li>它必须有一个明确的参数 (详见 <a class="external" href="http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/" rel="external nofollow">Incompatible <abbr title="ECMAScript 5th edition">ES5</abbr> change: literal getter and setter functions must now have exactly zero or one arguments</a>);</li>
<li>在对象字面量中,不能为一个已有真实值的变量使用 set ,也不能为一个属性设置多个 set。<br>
( <code>{ set x(v) { }, set x(v) { } }</code> 和 <code>{ x: ..., set x(v) { } }</code> 是不允许的 )</li>
</ul>
</div>
<h2 id="示例">示例</h2>
<h3 id="Example_Defining_a_getter_with_the_get_operator" name="Example:_Defining_a_getter_with_the_get_operator">在对象初始化时定义 setter</h3>
<p>这将定义一个对象 <code><font face="consolas, Liberation Mono, courier, monospace">language</font></code> 的伪属性<code>current</code>,当<code>current</code>被分配一个值时,将使用该值更新<code>log</code>:</p>
<pre class="brush: js">const language = {
set current(name) {
this.log.push(name);
},
log: []
}
language.current = 'EN';
console.log(language.log); // ['EN']
language.current = 'FA';
console.log(language.log); // ['EN', 'FA']</pre>
<p>请注意,<code>current</code>属性是未定义的,访问它时将会返回 <code>undefined</code>。</p>
<h3 id="Example_Removing_a_setter_with_the_delete_operator" name="Example:_Removing_a_setter_with_the_delete_operator">用 <code>delete</code> 操作符移除一个 setter</h3>
<p>我们可以使用<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a></code>操作符移除 setter。</p>
<pre class="brush: js">delete language.current;
</pre>
<h3 id="使用_defineProperty_为当前对象定义_setter">使用 <code>defineProperty</code> 为当前对象定义 setter</h3>
<p>我们可以随时使用 {{jsxref("Object.defineProperty()")}} 给一个已经存在的对象添加一个 setter。</p>
<pre class="brush: js">const 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</pre>
<h3 id="使用计算属性名">使用计算属性名</h3>
<pre class="brush: js">const expr = "foo";
const 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"
</pre>
<h2 id="规范"><span class="def"><span>规范</span></span></h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p>{{Compat("javascript.functions.set")}}</p>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="/zh-CN/docs/Web/JavaScript/Reference/Functions/get">getter</a></li>
<li>{{jsxref("Operators/delete", "delete")}}</li>
<li>{{jsxref("Object.defineProperty()")}}</li>
<li>{{jsxref("Object.defineGetter", "__defineGetter__")}}</li>
<li>{{jsxref("Object.defineSetter", "__defineSetter__")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters">Defining Getters and Setters</a> in JavaScript Guide</li>
</ul>
|