aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html
blob: 3f86e430497b058c3977d2bf052283ceb503d6ad (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
---
title: Object.getOwnPropertyNames()
slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
tags:
  - ECMAScript 5
  - JavaScript
  - JavaScript 1.8.5
  - Method
  - Object
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
---
<div>{{JSRef}}</div>

<p><strong><code>Object.getOwnPropertyNames()</code></strong>方法返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox">Object.getOwnPropertyNames(<em>obj</em>)</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>obj</code></dt>
 <dd>一个对象,其自身的可枚举和不可枚举属性的名称被返回。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>在给定对象上找到的自身属性对应的字符串数组。</p>

<h2 id="Description" name="Description">描述</h2>

<p><code>Object.getOwnPropertyNames()</code> 返回一个数组,该数组对元素是 <code>obj</code>自身拥有的枚举或不可枚举属性名称字符串。 数组中枚举属性的顺序与通过 {{jsxref("Statements/for...in", "for...in")}} 循环(或 {{jsxref("Object.keys")}})迭代该对象属性时一致。数组中不可枚举属性的顺序未定义。</p>

<h2 id="示例">示例</h2>

<h3 id="使用_Object.getOwnPropertyNames()">使用 <code>Object.getOwnPropertyNames()</code></h3>

<pre class="brush: js">var arr = ["a", "b", "c"];
console.log(Object.getOwnPropertyNames(arr).sort()); // ["0", "1", "2", "length"]

// 类数组对象
var obj = { 0: "a", 1: "b", 2: "c"};
console.log(Object.getOwnPropertyNames(obj).sort()); // ["0", "1", "2"]

// 使用Array.forEach输出属性名和属性值
Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
  console.log(val + " -&gt; " + obj[val]);
});
// 输出
// 0 -&gt; a
// 1 -&gt; b
// 2 -&gt; c

//不可枚举属性
var my_obj = Object.create({}, {
  getFoo: {
    value: function() { return this.foo; },
    enumerable: false
  }
});
my_obj.foo = 1;

console.log(Object.getOwnPropertyNames(my_obj).sort()); // ["foo", "getFoo"]
</pre>

<p>如果你只要获取到可枚举属性,查看{{jsxref("Object.keys")}}或用{{jsxref("Statements/for...in", "for...in")}}循环(还会获取到原型链上的可枚举属性,不过可以使用{{jsxref("Object.prototype.hasOwnProperty()", "hasOwnProperty()")}}方法过滤掉)。</p>

<p>下面的例子演示了该方法不会获取到原型链上的属性:</p>

<pre class="brush: js">function ParentClass() {}
ParentClass.prototype.inheritedMethod = function() {};

function ChildClass() {
  this.prop = 5;
  this.method = function() {};
}

ChildClass.prototype = new ParentClass;
ChildClass.prototype.prototypeMethod = function() {};

console.log(
  Object.getOwnPropertyNames(
    new ChildClass()  // ["prop", "method"]
  )
);
</pre>

<h3 id="只获取不可枚举的属性">只获取不可枚举的属性</h3>

<p>下面的例子使用了 {{jsxref("Array.prototype.filter()")}} 方法,从所有的属性名数组(使用<code>Object.getOwnPropertyNames()</code>方法获得)中去除可枚举的属性(使用{{jsxref("Object.keys()")}}方法获得),剩余的属性便是不可枚举的属性了:</p>

<pre class="brush: js">var target = myObject;
var enum_and_nonenum = Object.getOwnPropertyNames(target);
var enum_only = Object.keys(target);
var nonenum_only = enum_and_nonenum.filter(function(key) {
    var indexInEnum = enum_only.indexOf(key);
    if (indexInEnum == -1) {
        // 没有发现在enum_only健集中意味着这个健是不可枚举的,
        // 因此返回true 以便让它保持在过滤结果中
        return true;
    } else {
        return false;
    }
});

console.log(nonenum_only);</pre>

<pre>注:<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">Array.filter(filt_func)方法</a>创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。</pre>

<h2 id="Notes" name="Notes">提示</h2>

<p>在 ES5 中,如果参数不是一个原始对象类型,将抛出一个 {{jsxref("TypeError")}}  异常。在 ES2015 中,非对象参数被强制转换为对象 <strong></strong></p>

<pre class="brush: js">Object.getOwnPropertyNames('foo');
// TypeError: "foo" is not an object (ES5 code)

Object.getOwnPropertyNames('foo');
// ['length', '0', '1', '2']  (ES2015 code)
</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('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.8.5.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>

<p>{{Compat("javascript.builtins.Object.getOwnPropertyNames")}}</p>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<p>Firefox 28 {{geckoRelease("28")}}之前,<code>Object.getOwnPropertyNames</code> 不会获取到 {{jsxref("Error")}} 对象的属性。该 bug 在后面的版本修复了 ({{bug("724768")}})。</p>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li><a href="https://developer.mozilla.org/en-US/docs/Enumerability_and_ownership_of_properties" title="Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
 <li>{{jsxref("Object.prototype.hasOwnProperty")}}</li>
 <li>{{jsxref("Object.prototype.propertyIsEnumerable")}}</li>
 <li>{{jsxref("Object.create")}}</li>
 <li>{{jsxref("Object.keys")}}</li>
 <li>{{jsxref("Array.forEach()")}}</li>
</ul>