Revision 639775 of Object.getOwnPropertySymbols()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols
  • Revision title: Object.getOwnPropertySymbols()
  • Revision id: 639775
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Object")}} {{harmony}}

概述

Object.getOwnPropertySymbols() 方法会返回一个数组,该数组包含了指定对象自身的(非继承的)所有 symbol 属性键。

语法

Object.getOwnPropertySymbols(obj)

参数

obj
任意一个对象

描述

该方法和 {{jsxref("Object.getOwnPropertyNames()")}} 类似,但后者返回的结果只会包含字符串类型的属性键,也就是传统的属性名

示例

var obj = {};
var a = Symbol("a");
var b = Symbol.for("b");

obj[a] = "localSymbol";
obj[b] = "globalSymbol";

var objectSymbols = Object.getOwnPropertySymbols(obj);

console.log(objectSymbols.length); // 2
console.log(objectSymbols)         // [Symbol(a), Symbol(b)]
console.log(objectSymbols[0])      // Symbol(a)

规范

Specification Status Comment
{{SpecName('ES6', '#sec-object.getownpropertysymbols', 'Object.getOwnPropertySymbols')}} {{Spec2('ES6')}} Initial definition.

浏览器兼容性

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatGeckoDesktop("33.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatGeckoMobile("33.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

相关链接

  • {{jsxref("Object.getOwnPropertyNames()")}}
  • {{jsxref("Symbol")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}} {{harmony}}</div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>Object.getOwnPropertySymbols()</strong></code> 方法会返回一个数组,该数组包含了指定对象自身的(非继承的)所有 symbol 属性键。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code>Object.getOwnPropertySymbols(<em>obj</em>)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
 <dt>
  obj</dt>
 <dd>
  任意一个对象</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>该方法和 {{jsxref("Object.getOwnPropertyNames()")}} 类似,但后者返回的结果只会包含字符串类型的<strong>属性键</strong>,也就是传统的<strong>属性名</strong>。</p>
<h2 id="Examples">示例</h2>
<pre class="brush: js">
var obj = {};
var a = Symbol("a");
var b = Symbol.for("b");

obj[a] = "localSymbol";
obj[b] = "globalSymbol";

var objectSymbols = Object.getOwnPropertySymbols(obj);

console.log(objectSymbols.length); // 2
console.log(objectSymbols)         // [Symbol(a), Symbol(b)]
console.log(objectSymbols[0])      // Symbol(a)
</pre>
<h2 id="Specifications">规范</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-object.getownpropertysymbols', 'Object.getOwnPropertySymbols')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatGeckoDesktop("33.0") }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatGeckoMobile("33.0") }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
    <td>{{ CompatNo() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
 <li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
 <li>{{jsxref("Symbol")}}</li>
</ul>
Revert to this revision