diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/reflect/set/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/reflect/set/index.html | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/reflect/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/reflect/set/index.html new file mode 100644 index 0000000000..9cb7bbe280 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/reflect/set/index.html @@ -0,0 +1,98 @@ +--- +title: Reflect.set() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/set +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/set +--- +<div>{{JSRef}}</div> + +<p>静态方法 <code><strong>Reflect</strong></code><strong><code>.set()</code></strong> 工作方式就像在一个对象上设置一个属性。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Reflect.set(target, propertyKey, value[, receiver]) +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>target</code></dt> + <dd>设置属性的目标对象。</dd> + <dt><code>propertyKey</code></dt> + <dd>设置的属性的名称。</dd> + <dt><code>value</code></dt> + <dd>设置的值。</dd> + <dt><code>receiver</code></dt> + <dd>如果遇到 <code>setter</code>,<code>receiver</code>则为<code>setter</code>调用时的<code>this</code>值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回一个 {{jsxref("Boolean")}} 值表明是否成功设置属性。</p> + +<h3 id="异常">异常</h3> + +<p>抛出一个 {{jsxref("TypeError")}},如果目标不是 {{jsxref("Object")}}。</p> + +<h2 id="描述">描述</h2> + +<p><code>Reflect.set</code> 方法允许你在对象上设置属性。它的作用是给属性赋值并且就像 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">property accessor</a> 语法一样,但是它是以函数的方式。 </p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_Reflect.set">使用 <code>Reflect.set()</code></h3> + +<pre class="brush: js">// Object +var obj = {}; +Reflect.set(obj, "prop", "value"); // true +obj.prop; // "value" + +// Array +var arr = ["duck", "duck", "duck"]; +Reflect.set(arr, 2, "goose"); // true +arr[2]; // "goose" + +// It can truncate an array. +Reflect.set(arr, "length", 1); // true +arr; // ["duck"]; + +// With just one argument, propertyKey and value are "undefined". +var obj = {}; +Reflect.set(obj); // true +Reflect.getOwnPropertyDescriptor(obj, "undefined"); +// { value: undefined, writable: true, enumerable: true, configurable: true } +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-reflect.set', 'Reflect.set')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-reflect.set', 'Reflect.set')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Reflect.set")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Reflect")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">Property accessors</a></li> +</ul> |