aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/values/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/values/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/object/values/index.html113
1 files changed, 113 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/values/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/values/index.html
new file mode 100644
index 0000000000..42d9cced00
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/object/values/index.html
@@ -0,0 +1,113 @@
+---
+title: Object.values()
+slug: Web/JavaScript/Reference/Global_Objects/Object/values
+tags:
+ - JavaScript
+ - Method
+ - Object
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/values
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Object.values()</strong></code>方法返回一个给定对象自身的所有可枚举属性值的数组,值的顺序与使用{{jsxref("Statements/for...in", "for...in")}}循环的顺序相同 ( 区别在于 for-in 循环枚举原型链中的属性 )。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"><code>Object.values(<var>obj</var>)</code></pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>obj</code></dt>
+ <dd>被返回可枚举属性值的对象。</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>一个包含对象自身的所有可枚举属性值的数组。</p>
+
+<h2 id="描述">描述</h2>
+
+<p><code>Object.values()</code>返回一个数组,其元素是在对象上找到的可枚举属性值。属性的顺序与通过手动循环对象的属性值所给出的顺序相同。</p>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js">var obj = { foo: 'bar', baz: 42 };
+console.log(Object.values(obj)); // ['bar', 42]
+
+// array like object
+var obj = { 0: 'a', 1: 'b', 2: 'c' };
+console.log(Object.values(obj)); // ['a', 'b', 'c']
+
+// array like object with random key ordering
+// when we use numeric keys, the value returned in a numerical order according to the keys
+var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
+console.log(Object.values(an_obj)); // ['b', 'c', 'a']
+
+// getFoo is property which isn't enumerable
+var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } });
+my_obj.foo = 'bar';
+console.log(Object.values(my_obj)); // ['bar']
+
+// non-object argument will be coerced to an object
+console.log(Object.values('foo')); // ['f', 'o', 'o']</pre>
+
+<p class="brush: js"> </p>
+
+<h2 id="Polyfill">Polyfill </h2>
+
+<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">如果要 </span></font>Object.values</code>兼容不支持它的旧环境,可在<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: #eeeeee;"> </span></font><a href="https://github.com/tc39/proposal-object-values-entries">tc39/proposal-object-values-entries</a> 或 <a href="https://github.com/es-shims/Object.values">es-shims/Object.values</a> 中找到 Polyfill 。</p>
+
+<p>根据<strong>Object.keys()</strong>的Polyfill仿写一个:</p>
+
+<pre class="brush: js">if (!Object.values) Object.values = function(obj) {
+ if (obj !== Object(obj))
+ throw new TypeError('Object.values called on a non-object');
+ var val=[],key;
+ for (key in obj) {
+ if (Object.prototype.hasOwnProperty.call(obj,key)) {
+ val.push(obj[key]);
+ }
+ }
+ return val;
+}</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('ESDraft', '#sec-object.values', 'Object.values')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES8', '#sec-object.values', 'Object.values')}}</td>
+ <td>{{Spec2('ES8')}}</td>
+ <td> </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.builtins.Object.values")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
+ <li>{{jsxref("Object.keys()")}}</li>
+ <li>{{jsxref("Object.entries()")}} {{experimental_inline}}</li>
+ <li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li>
+ <li>{{jsxref("Object.create()")}}</li>
+ <li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
+</ul>